[英]android - Out of memory error when resizing images
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: 从Android开发人员复制了encodeSampledBitmapFromFile:
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. 我最大的问题是,代码可以在我的Motorola Milestone上正常运行,但是在Galaxy SIII上却收到此消息。 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. Android上存在一个与图像和视频元素的管理有关的众所周知的问题,无论是从相机捕获还是来自内部存储。
You can find additional information on several developer threads, as this one ( phonegap ) or this one ( apache ). 您可以在几个开发人员线程上找到更多信息,例如,这个( phonegap )或这个( apache )。 Unfortunately, there is no much a developer can do (AFAIK) in order to avoid these problems.
不幸的是,开发人员无法避免(AFAIK)为了避免这些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.