public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private class AsyncImageLoader extends AsyncTask<String, Void, Bitmap[]> {
Bitmap bitmap[];
protected void onPreExecute() {
pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));
showDialog();
}
@Override
protected Bitmap[] doInBackground(String... params) {
hideDialog();
bitmap = new Bitmap[2];
bitmap[0] = getBitmapFromURL(params[0]);
bitmap[1] = getBitmapFromURL(params[1]);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap[] bm) {
imgCover.setImageBitmap(bm[1]);
bm[1].recycle();
imgProfile.setImageBitmap(bm[0]);
bm[0].recycle();
if (MainActivity.PROFILE_UID.equals(MainActivity.USER_UID))
FragmentDrawer.imgProfileNavDrawer.setImageBitmap(bm[0]); // Sol drawer' da çıkan yuvarlak resmi güncellemek için
}
}
01-10 21:11:14.621 7906-8194/project.com.holobech E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) at java.util.concurrent.FutureTask.setException(FutureTask.java:219) at java.util.concurrent.FutureTask.run(FutureTask.java:239) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 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:528) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:600) at project.com.holobech.activity.ProfileFragment.getBitmapFromURL(ProfileFragment.java:508) at project.com.holobech.activity.ProfileFragment$AsyncImageLoader.doInBackground(ProfileFragment.java:529) at project.com.holobech.activity.ProfileFragment$AsyncImageLoader.doInBackground(ProfileFragment.java:516) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:856)
It works perfect for Android 5.0 and higher versions. But when i try with 4.2.2 version, that error occure. How can i fix this problem ? Thanks in advice