I develop a App with ffmpeg to decode a media frame. I filled a Bitmap object with the decode result and use ImageView.setImageBitmap to display the bitmap. In Android 2.3 it works well, but in Android 4.0 or up it doesn't work. The code is simply :
imgVedio.setImageBitmap(bitmapCache);//FIXME:in 4.0 it displays nothing
Then I tried write the Bitmap to a file and reload the file to 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);
It works, but the performance is too poor. So can someone help me resolve the problem?
I think it's an out of memory problem, you can fix it using 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 will return a smaller image and save memory. You can just call this in the ImageView.setImageBitmap :
imgVedio.setImageBitmap(loadImage(IMAGE_PATH));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With