2

I am facing issue with load bitmap image in one of my activity with cropper library. I am getting outofmemory error if image size is more than 5 MB...I have tried

android:largeHeap="true"

but it have not helped me for solve issue. My java code and logcat is like below

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)

as well my java code is like below

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

Please help me for solve issue.

Thanks

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
Rajubhai Rathod
  • 501
  • 2
  • 5
  • 14
  • May be a duplicate with http://stackoverflow.com/questions/25719620/how-to-solve-java-lang-outofmemoryerror-trouble-in-android – davidxxx Jul 09 '16 at 19:23
  • maybe your bitmap is too large or maybe it is not caused by this method, check your other codes to see if you have forgot to recycle object, especially bitmap. – Harlan Jul 09 '16 at 23:57

1 Answers1

1

compress is ineffective

try this when you use bitmap from file

 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;
}
Cgx
  • 753
  • 3
  • 6