Android 4.0 ImageView setImageBitmap does not work

落花浮王杯 提交于 2019-12-10 09:36:07

问题


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?


回答1:


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));


来源:https://stackoverflow.com/questions/12311288/android-4-0-imageview-setimagebitmap-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!