0
votes
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

1
Looks like an out of memory error in that logcat, which could explain why you get different results on different devices / Android APIs.NameSpace
i handle memory error, but first error still occuresLevent Tulun

1 Answers

2
votes

This is very common error once you deal with bitmap. Converting bitmap directly from URL is not a good practice. You should use bitmap BitmapFactory.Options feature provided by android by which you can get the size of the bitmap without even creating a bitmap. Now you have to use insamplesize of BitmapFactory Options and re size the images. I guess the size and the quality of image is too high and that's why when you are converting it into bitmap it gives you out of memory. Each Android app has max 50 mb of RAM to use once it cross beyond that limit android system throws out of memory exception. However we can manage it by making an entry of an attribute in manifest in application tag largeHeap to true.

The best practice to deal with android bitmap is available on developer site. You can click here

Note:- largeHeap feature will work only from OS level 3.0 and above.