我正在从XML解析图像并在我的Android应用程序中显示它.我的问题是当解析超过20个图像时出现以下错误...
06-09 14:28:37.710: ERROR/AndroidRuntime(490): java.lang.RuntimeException: An error occured while executing doInBackground()
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at java.lang.Thread.run(Thread.java:1096)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
06-09 14:28:37.710: ERROR/AndroidRuntime(490): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
Run Code Online (Sandbox Code Playgroud)
当我为我制作的文件编辑器缩略图像时,我遇到了同样的问题.你想要做的是在将图像存储到内存之前缩小图像,并在读取后从系统内存中处理源图像
这是我用于我的代码,它将图像缩小到110x110,但你可以设置自己的算法来缩放它(我相信在resizedbitmap类中实际上已有一个函数,但我还没有使用它)
public void setImageFromFile(String filename) {
ImageView fileImage = (ImageView)findViewById(R.id.file_image_holder);
Bitmap icon = BitmapFactory.decodeFile(filename);
int width = icon.getWidth();
int height = icon.getHeight();
int newWidth = 110;
int newHeight = 110;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix bMatrix = new Matrix();
bMatrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(icon, 0, 0, width, height, bMatrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
fileImage.setImageDrawable(bmd);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3524 次 |
| 最近记录: |