Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

android - OutOfMemoryException : Load Bunch Of Images From Server

I am developing an application which downloads Images from Server API...
We have created an API which gives JSON response of the image URL...

I have created GridView for displaying images and it is working successfully....

But the problem is that when the number of images increases it throws an OutOfMemoryException because of the increased heap memory...

I tried to clear heap memory manually by System.gc();.. but with no effect.......

So, my question is: How to load all images successfully from the server without having this issue.. What Changes should I make to solve this problem

There is my JSON and Code..

JSON :

{
is_error: "false",
is_error_msg: "Video/Image List",
images: [
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/Screenshot_2014-07-21-17-22-43.png",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/IMG_20140521_155703.jpg",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/IMG_20140522_152254.jpg",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/1386231199182.jpg",
     "http://equestsolutions.net/clients/elad/MovieApp/public/uploads/DSC01809.JPG"
],
videos: [ ],
others: [ ]
}

ImageAdapter.java

private class ImageAdapter extends BaseAdapter {

    private final Context mContext;
    ArrayList<String> urls_list;
    ProgressBar pbrLoadImage;

    public ImageAdapter(Context context,ArrayList<String> urls_list) {
        super();
        mContext = context;
        this.urls_list= urls_list;
    }

    @Override
    public int getCount() {
        return urls_list.size();
    }

    @Override
    public Object getItem(int position) {
        return urls_list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        ImageView imageView;

        if (convertView == null) { // if it's not recycled, initialize some attributes
            LayoutInflater mInflater = (LayoutInflater)
                    getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.image_grid_fragment_raw, null);
        }
        FrameLayout frm = (FrameLayout)convertView.findViewById(R.id.imageframe);
        pbrLoadImage =(ProgressBar)convertView.findViewById(R.id.pbrLoadImage);
        imageView = (ImageView)convertView.findViewById(R.id.imageFromUrl); ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.getLayoutParams().height = 160;
        imageView.getLayoutParams().width = 160;

        Log.i("values", "value :"+url_list.get(position));

        new BitmapWorkerTask(imageView).execute(url_list.get(position)); // calling class for download Images...
        return convertView;
    }

    //download Images from path
    class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        //private int data = 0;
        String path=null;
        Bitmap mIcon=null;

        public BitmapWorkerTask(ImageView imageView) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        // Decode image in background.
        @Override
        protected Bitmap doInBackground(String... params) {
            //data = params[0];
            path = params[0];

            InputStream in=null;
            ByteArrayOutputStream out=null;
            try {
                in = new java.net.URL(path).openStream();
                mIcon = BitmapFactory.decodeStream(in);
                out= new ByteArrayOutputStream();
                mIcon.compress(Bitmap.CompressFormat.PNG, 100, out);
                in.close();
                out.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            //return decodeSampledBitmapFromResource(getResources(), data, 100, 100));
            //return BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
            return mIcon;
        }

        // Once complete, see if ImageView is still around and set bitmap.
        @Override
        protected void onPostExecute(Bitmap bitmap) {

            if (imageViewReference != null && bitmap != null) {

                final ImageView imageView = imageViewReference.get();

                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }
}

Please tell me what changes I should make to solve my problem..

Thanks for your help in advance...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That's a pretty common problem in android. Loading bitmaps into memory may cause OutOfMemoryException.

I recommend you to use a lib like picasso, this lib loads images for you in background and resize them to fit your ImageView with one line of code.

Picasso.with(context).load(URL).fit().centerCrop().into(imageView);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...