bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos)给我java.lang.OutOfMemoryError

时间:2014-01-17 19:00:53

标签: java android performance base64 android-image

我正在使用此功能解码我的位图

public String BitMapToString(Bitmap bitmap){
            ByteArrayOutputStream baos=new  ByteArrayOutputStream();
            bitmap.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();
                bitmap.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;
      }

我从SO长篇上的帖子中得到了这个。这在Nexus上运行良好,但在三星上崩溃了 电话......我在一个异步任务中称之为但是它仍然让我感到崩溃...这里是异常的痕迹:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done in AsyncTask.java on Line 278
at java.util.concurrent.FutureTask$Sync.innerSetException in FutureTask.java on Line 273
at java.util.concurrent.FutureTask.setException in FutureTask.java on Line 124
at java.util.concurrent.FutureTask$Sync.innerRun in FutureTask.java on Line 307
at java.util.concurrent.FutureTask.run in FutureTask.java on Line 137
at android.os.AsyncTask$SerialExecutor$1.run in AsyncTask.java on Line 208
at java.util.concurrent.ThreadPoolExecutor.runWorker in ThreadPoolExecutor.java on Line 1076
at java.util.concurrent.ThreadPoolExecutor$Worker.run in ThreadPoolExecutor.java on Line 569
at java.lang.Thread.run in Thread.java on Line 856
Caused by: java.lang.OutOfMemoryError
at java.lang.String.<init> in String.java on Line 375
at java.lang.String.<init> in String.java on Line 238
at android.util.Base64.encodeToString in Base64.java on Line 456
at com.xxx.xxxx.activity.XXXActivity$SubmitData.BitMapToString in XXXActivity.java on Line 530
at com.xxx.xxxx.activity.XXXActivity$SubmitData.doInBackground in XXXActivity.java on Line 339
at com.xxx.xxxx.activity.XXXActivity$SubmitData.doInBackground in XXXActivity.java on Line 1
at android.os.AsyncTask$2.call in AsyncTask.java on Line 264
at java.util.concurrent.FutureTask$Sync.innerRun in FutureTask.java on Line 305
... 5 more
java.lang.OutOfMemoryError
at java.lang.String.<init> in String.java on Line 375
at java.lang.String.<init> in String.java on Line 238
at android.util.Base64.encodeToString in Base64.java on Line 456
at com.xxx.xxxx.activity.XXXActivity$SubmitData.BitMapToString in XXXActivity.java on Line 530
at com.xxx.xxxx.activity.XXXActivity$SubmitData.doInBackground in XXXActivity.java on Line 339
at com.xxx.xxxx.activity.XXXActivity$SubmitData.doInBackground in XXXActivity.java on Line 1
at android.os.AsyncTask$2.call in AsyncTask.java on Line 264
at java.util.concurrent.FutureTask$Sync.innerRun in FutureTask.java on Line 305
at java.util.concurrent.FutureTask.run in FutureTask.java on Line 137
at android.os.AsyncTask$SerialExecutor$1.run in AsyncTask.java on Line 208
at java.util.concurrent.ThreadPoolExecutor.runWorker in ThreadPoolExecutor.java on Line 1076
at java.util.concurrent.ThreadPoolExecutor$Worker.run in ThreadPoolExecutor.java on Line 569
at java.lang.Thread.run in Thread.java on Line 856

1 个答案:

答案 0 :(得分:1)

我会看一下Androids Developer文档,它们在加载大位图并将其缩小之前将它们保存到内存中。它可能不是您需要的解决方案,但我确信它可以帮助您指明正确的方向。

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

祝你好运!