I’m trying to resize some images and save them in another folder. Here’s the code:
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 is copied from android developers:
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);
}
My biggest problem is, that the code runs on my Motorola Milestone just fine, but on the Galaxy SIII I get this message.
Here is the exception message:
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
I tried several tips, but nothing really seams to work.
Thanks for any help!
There is a well known problem on Android related to the management of images and video elements, either captured from the camera or coming from the internal storage.
You can find additional information on several developer threads, as this one (phonegap) or this one (apache). Unfortunately, there is no much a developer can do (AFAIK) in order to avoid these problems.