android - 调整图像大小时出现内存不足错误

时间:2013-01-29 20:20:09

标签: java android image memory

我正在尝试调整某些图片的大小并将其保存在另一个文件夹中。这是代码:

 for(int i = 0; i < files.length; i++)
           {

               File image  = new File(direct, (String) files[i].subSequence(files[i].lastIndexOf("/"),files[i].length()));


               bitmap = ImageResizer.decodeSampledBitmapFromFile(files[i], targetWidth, targetHeight);

                    try {

                        outStream = new FileOutputStream(image);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 

                        outStream.flush();
                        outStream.close();
                        success = success && true;
                    } catch (FileNotFoundException e) {
                        success = false;
                        e.printStackTrace();
                    } catch (IOException e) {
                        success = false;
                        e.printStackTrace();
                    }
                    bitmap.recycle();

                    System.gc();
                    publishProgress((int) ((i / (float) count) * 100));

                     // Escape early if cancel() is called
                     if (isCancelled()) break;

           }

decodeSampledBitmapFromFile是从Android开发者复制的:

public static Bitmap decodeSampledBitmapFromFile(String filename,
        int reqWidth, int reqHeight, BitmapFactory.Options options) {

    // First decode with inJustDecodeBounds=true to check dimensions
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filename, options);
}

我最大的问题是,代码在我的摩托罗拉里程碑上运行得很好,但是在Galaxy SIII上我收到了这条消息。 以下是异常消息:

    01-29 21:12:54.709: E/dalvikvm-heap(24333): Out of memory on a 31961104-byte allocation.
    01-29 21:12:54.724: E/AndroidRuntime(24333): FATAL EXCEPTION: AsyncTask #2
    01-29 21:12:54.724: E/AndroidRuntime(24333): java.lang.RuntimeException: An error occured while executing doInBackground()
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.lang.Thread.run(Thread.java:856)
    01-29 21:12:54.724: E/AndroidRuntime(24333): Caused by: java.lang.OutOfMemoryError
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at         android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at imagegrid.ImageResizer.decodeSampledBitmapFromFile(ImageResizer.java:123)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at ResizeWorker$ResizeImages.doInBackground(ResizeWorker.java:120)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at ResizeWorker$ResizeImages.doInBackground(ResizeWorker.java:1)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    01-29 21:12:54.724: E/AndroidRuntime(24333):    ... 5 more

我尝试了几个技巧,但没有什么能真正发挥作用。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Android上存在一个与图像和视频元素管理相关的众所周知的问题,无论是从相机捕获还是来自内部存储。

您可以在多个开发人员线程中找到更多信息,例如(phonegap)或此一个(apache)。不幸的是,开发人员无法做多少(AFAIK)以避免这些问题。