我正在开发一个用户销售或购买二手商品的应用程序。在我的第一个活动中,我有网格视图,我在其中显示缩略图图像,当用户点击图像时,它会让他详细介绍活动。我使用后退按钮返回主活动页面。它工作正常但如果我点击8-9张图像并且内存不足(OOM)就会崩溃。
CustomVolleyRequest类
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
内存监控,每次点击都会增加,当我回到主要活动时它会释放内存!
堆栈追踪:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask E/AndroidRuntime:
Process: PID: 5700
java.lang.RuntimeException: An error occured while executing
doInBackground() E/AndroidRuntime: at
android.os.AsyncTask$3.done(AsyncTask.java:300) E/AndroidRuntime: at
java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime: at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime: Caused by: java.lang.OutOfMemoryError
E/AndroidRuntime: at
android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:627)
E/AndroidRuntime: at
com.example.xxx.xxx.DownloadImageTask.doInBackground(DownloadImageTask.java:26)
E/AndroidRuntime: at
com.example.xxx.xxx.DownloadImageTask.doInBackground(DownloadImageTask.java:14)
E/AndroidRuntime: at
android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime: at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:2)
根据日志,请看这一行:
E/AndroidRuntime: Caused by: java.lang.OutOfMemoryError
它明确表示应用程序因OutOfMemoryError
异常而崩溃。一个可能的原因可能是您正在处理大尺寸位图而不缩减它们。在将位图设置为图像视图之前,需要调整位图的大小和缩放。
有关如何有效管理位图的详细信息,请参阅官方docs