我正在使用带有ImageGetter的Textview来显示带图像的html。问题是如果因为位图占用太多内存而有很多图像我将面临内存问题。我应该怎么做才能防止这种情况?
这是我的ImageGetter代码:
private class ImageGetter implements Html.ImageGetter {
@Override
public Drawable getDrawable(String source) {
LevelListDrawable d = new LevelListDrawable();
Drawable empty = getResources().getDrawable(R.drawable.indicator_loading);
d.addLevel(0, 0, empty);
d.setBounds((size.x - size.x/5) / 2, 0, size.x/5, size.x/5);
new LoadImage().execute(source, d);
return d;
}
};
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private LevelListDrawable mDrawable;
@Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
mDrawable = (LevelListDrawable) params[1];
try {
return picasso.load(Uri.parse(source)).get();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
if (bitmap.getWidth() < 50) {
return;
}
BitmapDrawable d = new BitmapDrawable(resources, bitmap);
//
mDrawable.addLevel(1, 1, d);
int width = size.x * 90 / 100;
int height = bitmap.getHeight() * width / bitmap.getWidth();
mDrawable.setBounds((size.x - width) / 2, 0, width, height);
mDrawable.setLevel(1);
CharSequence t = textView.getText();
textView.setText(t);
}
}
}
Stacktrace:
致命异常:AsyncTask#17 java.lang.RuntimeException:执行doInBackground()时发生错误 在android.os.AsyncTask $ 3.done(AsyncTask.java:200) at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:274) at java.util.concurrent.FutureTask.setException(FutureTask.java:125) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:308) 在java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 在java.lang.Thread.run(Thread.java:1019) 引起:java.lang.OutOfMemoryError:位图大小超过VM预算 在android.graphics.BitmapFactory.nativeDecodeStream(本机方法) 在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470) 在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) 在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 在android.graphics.drawable.Drawable.createFromStream(Drawable.java:657) 在com.tornado.docbao24h.ui.ViewArcFragment $ URLImageParser $ ImageGetterAsyncTask.fetchDrawable(ViewArcFragment.java:436) 在com.tornado.docbao24h.ui.ViewArcFragment $ URLImageParser $ ImageGetterAsyncTask.doInBackground(ViewArcFragment.java:411) at com.tornado.docbao24h.ui.ViewArcFragment $ URLImageParser $ ImageGetterAsyncTask.doInBackground(ViewArcFragment.java:401) 在android.os.AsyncTask $ 2.call(AsyncTask.java:185) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306) 在java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 在java.lang.Thread.run(Thread.java:1019) 09-03 04:15:11.868 1729-1794 / com.tornado.docbao24h E / AndroidRuntime:致命异常:AsyncTask#18 java.lang.RuntimeException:执行doInBackground()时发生错误 在android.os.AsyncTask $ 3.done(AsyncTask.java:200) at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:274) at java.util.concurrent.FutureTask.setException(FutureTask.java:125) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:308) 在java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 在java.lang.Thread.run(Thread.java:1019) 引起:java.lang.OutOfMemoryError:位图大小超过VM预算 在android.graphics.BitmapFactory.nativeDecodeStream(本机方法) 在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470) 在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) 在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 在android.graphics.drawable.Drawable.createFromStream(Drawable.java:657) 在com.tornado.docbao24h.ui.ViewArcFragment $ URLImageParser $ ImageGetterAsyncTask.fetchDrawable(ViewArcFragment.java:436) 在com.tornado.docbao24h.ui.ViewArcFragment $ URLImageParser $ ImageGetterAsyncTask.doInBackground(ViewArcFragment.java:411) at com.tornado.docbao24h.ui.ViewArcFragment $ URLImageParser $ ImageGetterAsyncTask.doInBackground(ViewArcFragment.java:401) 在android.os.AsyncTask $ 2.call(AsyncTask.java:185) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306) 在java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 在java.lang.Thread.run(Thread.java:1019)