gpt4 book ai didi

Android 4.0 ImageView setImageBitmap 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:33 26 4
gpt4 key购买 nike

我用 ffmpeg 开发了一个应用程序来解码媒体帧。我用解码结果填充了一个 Bitmap 对象,并使用 ImageView.setImageBitmap 来显示位图。在 Android 2.3 中它运行良好,但在 Android 4.0 或更高版本中它不起作用。代码很简单:

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

然后我尝试将位图写入文件并重新加载文件以显示。

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

可以,但是性能太差了。那么有人可以帮我解决这个问题吗?

最佳答案

我认为这是内存不足的问题,您可以使用以下方法修复它:

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

“inSampleSize”选项将返回较小的图像并节省内存。您可以在 ImageView.setImageBitmap 中调用它:

imgVedio.setImageBitmap(loadImage(IMAGE_PATH));

关于Android 4.0 ImageView setImageBitmap 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com