我正在构建一些从网址加载图片的应用,当用户向下滚动应用时下载更多图片。
在大约30次下载后,我出现了内存异常。我在这里和Android开发人员中读到了它,但我找不到让它运行的方法。这是我的下载图片代码:
for (int k = 0; k < pictureAry.length(); k++) {
try {
URL url = new URL(picUrl + pictureAry.getString(k)
+ "/230");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
picArrayList.add(myBitmap);
break;
} catch (IOException e) {
Bitmap myBitmap = BitmapFactory.decodeResource(
context.getResources(),
R.drawable.icon_0404);
picArrayList.add(myBitmap);
}
}
这个用于展示:
enter codeint bwidth = picArrayList.get(0).getWidth();
int bheight = picArrayList.get(0).getHeight();
int swidth = mainTableForAllPosts.getWidth();
int sheight = mainTableForAllPosts.getHeight();
int new_width = swidth;
int new_height = (int) Math.floor((double) bheight
* ((double) new_width / (double) bwidth));
newbitMap = Bitmap.createScaledBitmap(
picArrayList.get(0), new_width, new_height, true);
mainImageView.setImageBitmap(newbitMap);
} else
mainImageView
.setImageBitmap(overlay(bitmapForVideoThumbnails));
} catch (Exception e) {
mainImageView.setImageResource(com.gy.bll.R.drawable.icon_0404);
}
它不写在这里,但我尝试使用bitmapFactory.options但我得到了堆栈可能是因为我的源代码是Url而不是文件。
这是日志猫:
06-27 12:30:10.008:E / dalvikvm(11265):内存不足:堆大小= 98304KB,分配= 92688KB,限制= 98304KB,Proc Limit = 98304KB 06-27 12:30:10.008:E / dalvikvm(11265):额外信息:足迹= 97504KB,允许的足迹= 98304KB,修剪= 1588KB 06-27 12:30:10.013:E / Bitmap_JNI(11265):创建位图失败。 06-27 12:30:10.013:E / Bitmap_JNI(11265):无法创建SkBitmap! 06-27 12:30:10.013:W / dalvikvm(11265):threadid = 16:线程退出未捕获异常(组= 0x41991a08) 06-27 12:30:10.013:E / AndroidRuntime(11265):致命异常:AsyncTask#5 06-27 12:30:10.013:E / AndroidRuntime(11265):java.lang.RuntimeException:执行doInBackground()时发生错误 06-27 12:30:10.013:E / AndroidRuntime(11265):在android.os.AsyncTask $ 3.done(AsyncTask.java:299) 06-27 12:30:10.013:E / AndroidRuntime(11265):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 06-27 12:30:10.013:E / AndroidRuntime(11265):at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 06-27 12:30:10.013:E / AndroidRuntime(11265):at java.util.concurrent.FutureTask.run(FutureTask.java:239) 06-27 12:30:10.013:E / AndroidRuntime(11265):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 06-27 12:30:10.013:E / AndroidRuntime(11265):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 06-27 12:30:10.013:E / AndroidRuntime(11265):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 06-27 12:30:10.013:E / AndroidRuntime(11265):at java.lang.Thread.run(Thread.java:864) 06-27 12:30:10.013:E / AndroidRuntime(11265):引起:java.lang.OutOfMemoryError :(堆大小= 98304KB,分配= 92688KB) 06-27 12:30:10.013:E / AndroidRuntime(11265):在android.graphics.Bitmap.nativeCreateScaledBitmap(原生方法) 06-27 12:30:10.013:E / AndroidRuntime(11265):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:809) 06-27 12:30:10.013:E / AndroidRuntime(11265):at com.gy.bll.JsonParser.makeGuiRow(JsonParser.java:516) 06-27 12:30:10.013:E / AndroidRuntime(11265):at com.gy.bll.JsonParser.access $ 1(JsonParser.java:332) 06-27 12:30:10.013:E / AndroidRuntime(11265):at com.gy.bll.JsonParser $ jsonPostAsyncTask.doInBackground(JsonParser.java:299) 06-27 12:30:10.013:E / AndroidRuntime(11265):at com.gy.bll.JsonParser $ jsonPostAsyncTask.doInBackground(JsonParser.java:1) 06-27 12:30:10.013:E / AndroidRuntime(11265):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 06-27 12:30:10.013:E / AndroidRuntime(11265):at java.util.concurrent.FutureTask.run(FutureTask.java:234) 06-27 12:30:10.013:E / AndroidRuntime(11265):... 4更多
现在我使用android:largeHeap =“true”在我的清单中让我的应用程序不会崩溃但是我在日志中看到,heep grwos到250m +并且它使我的应用程序运行缓慢。
感谢。
答案 0 :(得分:0)