1

First I'm creating an Arraylist of images and image information by parsing json response

`JSONArray jsonArray = jsonObj.getJSONArray("result");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObjInner = jsonArray.getJSONObject(i);

                    ImageInfo imageInfo = new ImageInfo();
                    imageInfo.setImageUrl("http://www.dvimaytech.com/markphoto/" + jsonObjInner.getString("image"));

                    new GetDrawableFromUrl(imageInfo).execute();

                    imageInfo.setEmail(jsonObjInner.getString("emailid"));
                    imageInfo.setImage_id(jsonObjInner.getString("image_id"));
                    imageInfo.setImage_name(jsonObjInner.getString("image_name"));
                    imageInfo.setAmount(jsonObjInner.getString("amount"));
                    imageInfo.setImage_description(jsonObjInner.getString("image_description"));
                    imageInfo.setDate_created(jsonObjInner.getString("date_created"));
                }`

Then I get images from server another in Async task using the code

public class GetDrawableFromUrl extends AsyncTask<Void, Void, Drawable> {
    public ImageInfo imageInfoObj;

    GetDrawableFromUrl(ImageInfo imageInfo) {
        imageInfoObj = imageInfo;
    }

    @Override
    protected Drawable doInBackground(Void... params) {
        try {
            return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(imageInfoObj.getImageUrl()).getContent()), "src_name");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Drawable drawable) {
        if (drawable != null) {
            imageInfoObj.setImage(drawable);
            listImageInfo.add(imageInfoObj);
            showImagesInList();
        }
    }
}

My adapter class code

public class MyImageListAdapter extends BaseAdapter {

    private LayoutInflater inflater = null;

    public MyImageListAdapter() {
        inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return listImageInfo.size();
    }

    public Object getItem(int position) {
        return listImageInfo.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_row, null);
        }

        TextView tvName = (TextView) vi.findViewById(R.id.tv_name);
        TextView tvTime = (TextView) vi.findViewById(R.id.tv_time);
        ImageView image = (ImageView) vi.findViewById(R.id.iv_image);
        final Button btnDelete = (Button) vi.findViewById(R.id.btn_delete);

        image.setImageDrawable(listImageInfo.get(position).getImage());

        tvName.setText("Name: " + listImageInfo.get(position).getImage_name());
        tvTime.setText("Date: " + listImageInfo.get(position).getDate_created());

        btnDelete.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                final int position = listView.getPositionForView((View) v.getParent());
                positionOgBtnToDelete = position;
                Log.v("delete btn clicked", "delete btn no: " + position);
                Toast.makeText(HomeActivity.this, "Btn delete position: " + position, Toast.LENGTH_LONG).show();
                showAlertToConfirmDelete();
            }
        });
        return vi;
    }
}

Now my list only contains 5-6 images. But I'm getting error OutOfMemory error on line

return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(imageInfoObj.getImageUrl()).getContent()), "src_name");

My log - cat is

 E/dalvikvm-heap(10927): 7680000-byte external allocation too large for this process.
: D/dalvikvm(10927): GC_FOR_MALLOC freed 0K, 55% free 3286K/7303K, external     54717K/56765K, paused 32ms
: E/GraphicsJNI(10927): VM won't let us allocate 7680000 bytes
: D/skia(10927): libjpeg error 105 <  Ss=%d, Se=%d, Ah=%d, Al=%d> from allocPixelRef [1600 1200]
 D/skia(10927): --- decoder->decode returned false
 W/dalvikvm(10927): threadid=9: thread exiting with uncaught exception (group=0x40018578)
 E/AndroidRuntime(10927): FATAL EXCEPTION: AsyncTask #1
 E/AndroidRuntime(10927): java.lang.RuntimeException: An error occured while executing doInBackground()
 E/AndroidRuntime(10927):   at android.os.AsyncTask$3.done(AsyncTask.java:200)
 E/AndroidRuntime(10927):   at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
 E/AndroidRuntime(10927):   at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
 E/AndroidRuntime(10927):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
 E/AndroidRuntime(10927):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 E/AndroidRuntime(10927):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 E/AndroidRuntime(10927):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 E/AndroidRuntime(10927):   at java.lang.Thread.run(Thread.java:1019)
 E/AndroidRuntime(10927): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
 E/AndroidRuntime(10927):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
 E/AndroidRuntime(10927):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
 E/AndroidRuntime(10927):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
 E/AndroidRuntime(10927):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
 E/AndroidRuntime(10927):   at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
 E/AndroidRuntime(10927):   at com.markphoto_activities.DisplayImagesActivity$GetDrawableFromUrl.doInBackground(DisplayImagesActivity.java:257)
 E/AndroidRuntime(10927):   at com.markphoto_activities.DisplayImagesActivity$GetDrawableFromUrl.doInBackground(DisplayImagesActivity.java:1)
 E/AndroidRuntime(10927):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
 E/AndroidRuntime(10927):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
 E/AndroidRuntime(10927):   ... 4 more
 D/dalvikvm(10927): GC_EXTERNAL_ALLOC freed 122K, 55% free 3329K/7303K, external 55034K/56765K, paused 49ms
 E/dalvikvm-heap(10927): 4194304-byte external allocation too large for this process.
 D/dalvikvm(10927): GC_FOR_MALLOC freed <1K, 55% free 3329K/7303K, external 55034K/56765K, paused 32ms
 E/GraphicsJNI(10927): VM won't let us allocate 4194304 bytes

Instead of this, I'm showing the same list on another activity, where I'm doing the same process.

Also there are other two activities where 1. I capture image using camera intent and upload to server(calling api and using Http Multipart Entity), and 2. I upload images from gallery.

This is not the only place I'm getting this error, in fact a lot at other places. But 90% error comes on this line.

I want to know, whether the problem is coming because of a single large size image of all images together taking more memory. And how to remove it?

5
  • In the ArrayList I'm storing images in Drawable format. Is there any other way that I can store them, because with 5-6 images i'm strugling, and supposed to show 100-200 images in list Commented Jan 3, 2013 at 8:10
  • What is the size of the images you are using Commented Jan 3, 2013 at 9:07
  • 1
    if your are running this on os version greater or equal to 3.0 then you can use this ` android:largeHeap="true"` in your manifest at application level. Commented Jan 3, 2013 at 9:56
  • @almuneef actually I don't know is the size, but actually the user can upload image captured using camera or any image from gallery, so it can be of any size Commented Jan 3, 2013 at 10:06
  • @AliImran If it works, then its a very useful information, thanks Commented Jan 3, 2013 at 10:27

1 Answer 1

1

Use a Image loader You can find an example here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.