java.lang.OutOfMemoryError : Android 错误

标签 java android bitmap android-logcat

我在使用 cropper 库的一项 Activity 中遇到加载位图图像的问题。如果图像大小超过 5 MB,我会遇到内存不足的错误...我试过了

android:largeHeap="true"

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

日志:

07-09 21:10:47.482: E/art(4870): Throwing OutOfMemoryError "Failed to allocate a 6391674 byte allocation with 6004224 free bytes and 5MB until OOM" 07-09 21:10:47.482: D/AndroidRuntime(4870): Shutting down VM 07-09 21:10:47.522: E/AndroidRuntime(4870): FATAL EXCEPTION: main 07-09 21:10:47.522: E/AndroidRuntime(4870): Process: com.karopass.status2017, PID: 4870 07-09 21:10:47.522: E/AndroidRuntime(4870): java.lang.OutOfMemoryError: Failed to allocate a 6391674 byte allocation with 6004224 free bytes and 5MB until OOM 07-09 21:10:47.522: E/AndroidRuntime(4870): at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122) 07-09 21:10:47.522: E/AndroidRuntime(4870): at com.karopass.status2017.material.ImageLoader.addToDiskCache(ImageLoader.java:238) 07-09 21:10:47.522: E/AndroidRuntime(4870): at 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;
}

请帮我解决问题。

谢谢

最佳答案

压缩无效

当你使用来自文件的位图时试试这个

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

关于java.lang.OutOfMemoryError : Android 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38283538/

相关文章:

java - 动态替换某些字符串值

java - 第二个循环背后的逻辑

java - 通俗地说,Spring中的getBean是干什么的?

android - 实时数据。无法分配给 ‘value’ : the setter is protected/*protected and package*/for synthetic extension

android - BitmapFactory.decodeFile 导致 outOfMemoryError

java - 为什么android Bitmap类不使用new关键字?

java - Process.waitfor 会使服务/Activity 进入休眠状态还是会消耗 CPU?

android - 我们能保证 Android 的 SpeechRecognizer 不会向 Google 发送数据吗?

android - 如何从本地json文件中获取数据?

java - 在带有彩色背景的 Canvas 上绘制位图(来自可绘制对象)