android - 索尼爱立信 Xperia 上的虚拟内存不足

标签 android

我的应用程序包含大量在运行时加载的图像。这个应用程序几乎在所有安卓设备上都能正常工作,但在 Xperia 上它经常因虚拟内存不足而崩溃。 请帮助我...

最佳答案

您需要捕获低内存错误并减小下载图像的大小,如下所示。

 catch(OutOfMemoryError e)
         {
             e.printStackTrace();
             Log.e("Out of memory error", e.toString());
             reduce_size();
         }
.....

BitmapFactory.Options options=new BitmapFactory.Options();
    options.inSampleSize = 8;
    options.inJustDecodeBounds = true;

    Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

    final int REQUIRED_SIZE=70;

    int width_tmp=options.outWidth, height_tmp=options.outHeight;
    int scale=1;
    while(true){
        if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
            break;
        width_tmp/=2;
        height_tmp/=2;
        scale*=2;
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize=scale;
    Bitmap btm=BitmapFactory.decodeStream(is, null, o2);
    img_t.setImageBitmap(btm);

关于android - 索尼爱立信 Xperia 上的虚拟内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9494554/

相关文章:

android - 一个 Activity 问题中的相同 fragment

android - 操作栏项目重复

android - 如何在android中使用ffmpeg合并多个mp3文件

android - Sencha 触摸 : Disclosure on single item

java - 如何在 Android java 中正确使用 smbj 连接并列出 samba 共享上的文件?

Android如何检测拨出电话是否被接听

java - 在 Activity 之间传输数据

android - 自定义单选按钮图像未填充空间

android - 我可以通过 wifi 唤醒我的 Android 设备吗?

java - 使用 com.github.clans.fab.FloatingActionButton 时命名空间 'fab' 未绑定(bind)错误