Android 4.0 ImageView setImageBitmap not working

I am developing an application with ffmpeg for decoding a media frame. I populated the Bitmap object with the decoding result and used ImageView.setImageBitmap to display the bitmap. It works well in Android 2.3, but it doesn’t work in Android 4.0 or higher. The code is simple:

imgVedio.setImageBitmap(bitmapCache);//FIXME:in 4.0 it displays nothing 

Then I tried to write Bitmap to a file and reload the file for display.

 String fileName = "/mnt/sdcard/myImage/video.jpg"; FileOutputStream b = null; try { b = new FileOutputStream(fileName); bitmapCache.compress(Bitmap.CompressFormat.JPEG, 100, b);// write data to file } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if(b != null) { b.flush(); b.close(); } } catch (IOException e) { e.printStackTrace(); } } Bitmap bitmap = BitmapFactory.decodeFile(fileName); imgVedio.setImageBitmap(bitmap); 

This works, but the performance is too poor. So can someone help me solve the problem?

+4
source share
1 answer

I think this is a memory problem, you can fix it with this method:

 private Bitmap loadImage(String imgPath) { BitmapFactory.Options options; try { options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options); return bitmap; } catch(Exception e) { e.printStackTrace(); } return null; } 

The inSampleSize option returns a smaller image and saves memory. You can simply call this in ImageView.setImageBitmap:

 imgVedio.setImageBitmap(loadImage(IMAGE_PATH)); 
+14
source

Source: https://habr.com/ru/post/1432851/


All Articles