1
我試圖從url異步方法調用圖像。它會在後臺執行內存不足錯誤。我的代碼如下Asynctask中的圖像OutOfMemoryError
代碼:
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