Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.0 ImageView setImageBitmap does not work

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?

like image 726
Aborigine In Town Avatar asked Dec 08 '22 21:12

Aborigine In Town


1 Answers

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));
like image 142
vbonnet Avatar answered Jan 02 '23 10:01

vbonnet