java.lang.OutOfMemoryError:Android

拉古拜·拉索德(Rajubhai Rathod)

我在cropper库中的一项活动中遇到了加载位图图像的问题。如果图像大小超过5 MB,我将收到内存不足错误...我已经尝试过

android:largeHeap="true"

但这并没有帮助我解决问题。我的Java代码和logcat如下所示

Logcat:

07-09 21:10:47.482:E / art(4870):抛出OutOfMemoryError“无法分配6391674字节分配,其中6004224空闲字节和5MB直到OOM” 07-09 21:10:47.482:D / AndroidRuntime(4870) :关闭VM 07-09 21:10:47.522:E / AndroidRuntime(4870):致命异常:主07-09 21:10:47.522:E / AndroidRuntime(4870):进程:com.karopass.status2017,PID: 4870 07-09 21:10:47.522:E / AndroidRuntime(4870):java.lang.OutOfMemoryError:无法分配6391674字节分配与6004224可用字节和5MB,直到OOM 07-09 21:10:47.522:E / AndroidRuntime (4870):位于java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)07-09 21:10:47.522:E / AndroidRuntime(4870):位于com.karopass.status2017.material.ImageLoader.addToDiskCache(ImageLoader。 java:238)07-09 21:10:47.522:E / AndroidRuntime(4870):位于com.karopass.status2017。material.ImageLoader.saveTempImage(ImageLoader.java:271)

以及我的Java代码如下

public File addToDiskCache(String imageName, Bitmap inImage) {

    Log.e("Add to Disk",imageName);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    if(!mCacheDir.exists()){
        mCacheDir.mkdirs();
    }

    File filePath=new File(mCacheDir.getPath()+File.separator+imageName);
    if(!filePath.exists()) {
        try {
            filePath.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    OutputStream os=null;
    try {
           os= new FileOutputStream(filePath.getPath());
            os.write(bytes.toByteArray());
            os.flush();
        } catch (IOException e) {
            Log.e("Write err",filePath.toString());
        }finally {
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Log.e("storage path",filePath.toString());

    return filePath;
}

请帮我解决问题。

谢谢

Cgx

压缩无效

当您使用文件中的位图时,请尝试此操作

 private Bitmap sampleImage(Bitmap bitmap, int reqWidth, int reqHight) {


        //first decode
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile("FILE_PATH",options);
        //seconde deconde
        options.inSampleSize = caculateSampleSize(options,reqWidth,reqHight);
        options.inJustDecodeBounds = false;


        bitmap = BitmapFactory.decodeFile("FILE_PATH",options);
         return bitmap;

    }

private int caculateSampleSize(BitmapFactory.Options options, int reqWidth, int reqHight) {
    int screenWidth;
    int screenHight;
    screenWidth = (reqWidth == 0 ? this.getResources().getDisplayMetrics().widthPixels : reqWidth);
    screenHight = (reqHight == 0 ? this.getResources().getDisplayMetrics().heightPixels : reqHight);
    int sampleWith = options.outWidth / screenWidth;
    int sampleHight = options.outHeight / screenHight;
    //use max
    return  sampleWith > sampleHight ? sampleWith : sampleHight;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android java.lang.OutOfMemoryError?

如何清除Android PDFView中的java.lang.OutOfMemoryError?

带有大量图像的android java.lang.OutOfMemoryError recyclerview

Android Robolectric Gradle-java.lang.OutOfMemoryError:PermGen空间

android应用程序中的java.lang.OutOfMemoryError

如何解决Android中的java.lang.OutOfMemoryError问题

Android java.lang.OutOfMemoryError仅在Samsung设备上

java.lang.OutOfMemoryError:位图大小超出VM预算-Android

ListView android滚动期间出现java.lang.OutOfMemoryError

如何在Android中修复java.lang.OutOfMemoryError

Xamarin android 部署上的 Java,Lang.OutOfMemoryError

Android Studio:E/UncaughtException:java.lang.OutOfMemoryError

捕获java.lang.OutOfMemoryError?

java.lang.OutOfMemoryError图片

错误是java.lang.OutOfMemoryError

java.lang.OutOfMemoryError:始终无法分配JNI Env android java

java.lang.OutOfMemoryError:Java堆空间

java.lang.OutOfMemoryError:Java堆空间

java.lang.OutOfMemoryError:Java堆空间?

EDF文件解析时Android应用程序运行到java.lang.OutOfMemoryError

java.lang.OutOfMemoryError:Android 1.4超出了GC开销限制

Android:创建图库应用程序时出现java.lang.OutOfMemoryError

任何想法如何修复java.lang.outofmemoryerror android listview错误?

致命异常:java.lang.OutOfMemoryError:EnsureLocalCapacity (FileInputStream.open0, android.graphics.Typeface.getFullFlipFont)

java.lang.OutOfMemoryError: [内存耗尽] 从 Sqlite Android 读取数据时

java.lang.OutOfMemoryError:PermGen空间异常

java.lang.OutOfMemoryError:PermGen空间

java.lang.OutOfMemoryError:分配失败

Eclipse:java.lang.OutOfMemoryError:PermGen空间