У меня есть ошибка при загрузке нескольких изображений, Изображение успешно загружается, но оно не может открыться дважды. если я закрыть деятельность и возобновить деятельность я получаю ошибкуОШИБКА AsyncTask # 1 во время выполнения doInBackground И OutOfMemoryError
это ошибка:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
at com.metroapp.lazyloader.ImageLoader.decodeFile(ImageLoader.java:181)
at com.metroapp.lazyloader.ImageLoader.getBitmap(ImageLoader.java:78)
at com.metroapp.activity.CatalogsDetailActivity$GetImageAsyncTask.doInBackground(CatalogsDetailActivity.java:440)
at com.metroapp.activity.CatalogsDetailActivity$GetImageAsyncTask.doInBackground(CatalogsDetailActivity.java:429)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
это мой код:
public class GetImageAsyncTask extends AsyncTask<Void, Void, String> {
String bd_image;
public GetImageAsyncTask(String bd_image) {
this.bd_image=bd_image;
load = new ImageLoader(ctx);
}
@Override
protected String doInBackground(Void... params) {
try {
image = load.getBitmap(bd_image);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
arr.add(image);
imageArray.remove(0);
try {
if (imageArray.size() > 0) {
new GetImageAsyncTask(imageArray.get(0)).execute();
}else{
loadData();
}
}catch (Exception e){
e.printStackTrace();
loadData();
}
}
}
и
private Bitmap decodeFile(File f) {
Bitmap bitmap = null;
FileInputStream stream1= null;
FileInputStream stream2= null;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
for (o2.inSampleSize = 1; o2.inSampleSize <= 32; o2.inSampleSize++) {
try {
stream2 = new FileInputStream(f);
bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
Log.d("DECODE FILE : ", "Decoded successfully for sampleSize " + o2.inSampleSize);
break;
} catch (OutOfMemoryError outOfMemoryError) {
// If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
Log.e("DECODE FILE : ", "outOfMemoryError while reading file for sampleSize " + o2.inSampleSize + " retrying with higher value");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
любое решение?
http://stackoverflow.com/a/25719828/7320259 проверить это –
Проблема с кучей памяти. Проверьте это http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object?rq=1 – Shailesh
, вам нужно изменить размер растрового изображения перед загрузкой в объект Bitmap –