我尝试从url异步方法调用图像。它在后台生成内存不足错误。我的代码在
之下代码:
class DownloadAsyncTask extends AsyncTask<ViewHolder1, Void, ViewHolder1> {
@Override
protected ViewHolder1 doInBackground(ViewHolder1... params) {
// TODO Auto-generated method stub
// load image directly
ViewHolder1 viewHolder = params[0];
try {
String url = viewHolder.imageURL.replace(" ", "%20");
URL imageURL = new URL(url);
Log.v("Image Url", viewHolder.imageURL);
viewHolder.bitmap = BitmapFactory.decodeStream(imageURL.openStream());
} catch (IOException e) {
// TODO: handle exception
Log.e("error", "Downloading Image Failed");
viewHolder.bitmap = null;
}
return viewHolder;
}
@Override
protected void onPostExecute(ViewHolder1 result) {
// TODO Auto-generated method stub
Log.v("Result bitmap", String.valueOf(result.bitmap));
if (result.bitmap == null) {
result.frndthumb_image
.setImageResource(R.drawable.avatar_image);
} else {
result.frndthumb_image.setImageBitmap(result.bitmap);
}
}
}
错误代码:
03-13 13:40:34.170: E/AndroidRuntime(1919): FATAL EXCEPTION: AsyncTask #3
03-13 13:40:34.170: E/AndroidRuntime(1919): java.lang.RuntimeException: An error occured while executing doInBackground()
03-13 13:40:34.170: E/AndroidRuntime(1919): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.lang.Thread.run(Thread.java:856)
03-13 13:40:34.170: E/AndroidRuntime(1919): Caused by: java.lang.OutOfMemoryError
03-13 13:40:34.170: E/AndroidRuntime(1919): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-13 13:40:34.170: E/AndroidRuntime(1919): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
03-13 13:40:34.170: E/AndroidRuntime(1919): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:549)
03-13 13:40:34.170: E/AndroidRuntime(1919): at com.arrichion.userservices.ChallengesAdapter$DownloadAsyncTask.doInBackground(ChallengesAdapter.java:133)
03-13 13:40:34.170: E/AndroidRuntime(1919): at com.arrichion.userservices.ChallengesAdapter$DownloadAsyncTask.doInBackground(ChallengesAdapter.java:1)
03-13 13:40:34.170: E/AndroidRuntime(1919): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-13 13:40:34.170: E/AndroidRuntime(1919): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 13:40:34.170: E/AndroidRuntime(1919): ... 4 more
答案 0 :(得分:0)
我建议您使用A Query下载images.just从下面的链接下载jar文件
https://code.google.com/p/android-query/downloads/detail?name=android-query.0.26.7.jar
并使用
调用此方法AQuery _aq = new AQuery(youractivity context);
//fetch and set the image from internet, cache with file and memory
aq.id(R.id.Your imageview object).image("http://www.vikispot.com/z/images/vikispot/android-w.png");