1

i got this exception

 Process: ua.khuta.mobilereception, PID: 7601
    java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)
    Caused by: java.lang.OutOfMemoryError
    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
    at java.lang.StringBuilder.append(StringBuilder.java:216)
    at ua.khuta.mobilereception.ReportAboutProblem$Report.doInBackground(ReportAboutProblem.java:360)
    at ua.khuta.mobilereception.ReportAboutProblem$Report.doInBackground(ReportAboutProblem.java:347)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

The exception is on this line:

  files+="[\""+encodeTobase64(myImages.get(i))+"\",\""+i+"image.jpg\"]";

myImages - array list of Bitmaps

public static String encodeTobase64(Bitmap image) {
    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG,100, baos);
    byte [] b=baos.toByteArray();
    String temp=null;
    try{
        System.gc();
        temp=Base64.encodeToString(b, Base64.DEFAULT);
    }catch(Exception e){
        e.printStackTrace();
    }catch(OutOfMemoryError e){
        baos=new  ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG,50, baos);
        b=baos.toByteArray();
        temp=Base64.encodeToString(b, Base64.DEFAULT);
        Log.e("EWN", "Out of memory error catched");
    }
    return temp;
}

It depends from photo, when i load 3 photo from camera to bitmap array - all is good, when 3 photo from galery - a get error.

Kostya Khuta
  • 1,846
  • 6
  • 26
  • 48

2 Answers2

0

Please refer to official docs for this problem. you will find a proper solution here:

http://developer.android.com/training/displaying-bitmaps/index.html

Abdul Mohsin
  • 1,253
  • 1
  • 13
  • 24
  • no, a get this exception in encoding, not in displaying image – Kostya Khuta Apr 09 '14 at 09:06
  • if you read that document then you will understand the exact problem. that is for encoding not for displaying also. but you didn't read that....http://developer.android.com/training/displaying-bitmaps/load-bitmap.html this is what exactly you need – Abdul Mohsin Apr 09 '14 at 10:52
0

you can request to use more memory by using

 android:largeHeap="true"

in the manifest.

also, you can use native memory (NDK & JNI) , so you actually bypass the heap size limitation.

here are some posts made about it:

and here's a library made for it:

happy coding

regards maven

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89