Android:Bitmap

时间:2015-10-16 05:55:06

标签: android

我收到错误 java.lang.OutOfMemoryError 并在运行应用程序时崩溃应用程序,从服务器下载多个图像并存储到SD卡中。我在gradle中有一些更改.properties并更改行#org.gradle.jvmargs = -Xmx2048m -XX:MaxPermSize = 1024m -XX:MaxHeapSize \ = 1024m -XX:+ HeapDumpOnOutOfMemoryError -Xmx1024m -Dfile.encoding = UTF-8 。并尝试调用 System.gc(); 。但是一次又一次地得到相同的错误。如何解决这个问题。谢谢你提前。

这是我的代码

此行的错误 imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(),null,options);

Log.e("fileUrl ", " = " + fileUrl + " ImageName = " + ImageName);
            URL ImgUrl = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
            conn.connect();
            int lenghtOfImage_File = conn.getContentLength();
            Log.e("lenghtOfImage_File ", " = " + lenghtOfImage_File);


                System.gc();
                if(imagenObtenida != null)
                {
                    imagenObtenida.recycle();
                    imagenObtenida = null;
                }
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inTempStorage = new byte[16*1024];
                options.inPurgeable = true;
                options.inSampleSize = 1;

                imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);
                newFolder = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "classnkk_images");

                File file = new File(newFolder, ImageName);

这是我的日志错误

10-16 11:16:20.978    1135-1152/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:278)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
            at com.example.tazeen.classnkk.AllPosts_Page.download_PngFile(AllPosts_Page.java:1128)
            at com.example.tazeen.classnkk.AllPosts_Page.getDoenLoaddata(AllPosts_Page.java:735)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:705)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:701)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

2 个答案:

答案 0 :(得分:2)

java.lang.OutOfMemoryError: 请求的大小超过了VM限制。 此错误表示Java应用程序尝试ti分配一个数组,其大小大于堆大小。

OutOfMemoryError扩展了VirtualMachineError 类,该类指示JVM已损坏,或者资源已耗尽且无法运行。

  

确认您的应用程序不存储不必要的信息。   仅存储和维护所需的那些信息   正确执行Java应用程序。

Strange out of memory issue while loading an image to a Bitmap object

答案 1 :(得分:0)

您尝试从服务器下载的图像可能很大,因此在尝试解码时,会导致OOM异常。

在开发者网站上查看此帖子,以处理位图here