我正在尝试调整某些图片的大小并将其保存在另一个文件夹中。这是代码:
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
我尝试了几个技巧,但没有什么能真正发挥作用。
感谢您的帮助!