android how to download an 1mb image file and set to ImageView

后端 未结 1 1054
臣服心动
臣服心动 2021-01-05 22:52

I am trying to download an 1mb image file and then save to bitmap to populate an imageview. but shows error

06-13 13:39:48.149: ERROR/AndroidRuntime(1782):
j         


        
相关标签:
1条回答
  • 2021-01-05 23:32

    I had been through the same issue. Before you set the downloaded bitmap to your ImageView, you have to compress your bitmap according to the width and height of your ImageView.

    You have to create a scaled bitmap like this,

     bm1=Bitmap.createScaledBitmap(bm, 300, 300,true);
    imgView.setImageBitmap(bm1);
    

    or Compress your bitmap like this,

                OutputStream fOut = null;
                            File file = new File(strDirectoy,imgname);
                                fOut = new FileOutputStream(file);
    
                            bm1.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                                fOut.flush();
                                fOut.close();
    
                    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    
    0 讨论(0)
提交回复
热议问题