OutOfMemoryException异常:加载束从服务器映像映像、加载、异常、服务器

2023-09-12 08:14:38 作者:余生请多指教

我在开发的图片下载从服务器API的应用程序... 我们已经创建了一个API,它使图像URL的JSON响应...

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

我已经创建的GridView 用于显示图像,这是工作顺利......

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

但问题是,当图像的数量会增加它抛出一个 OutOfMemoryException异常增加的堆内存,因为...

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

我试图通过手动清除堆内存的System.gc(); ..但没有效果.......

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

还有就是我的JSON和code ..

There is my JSON and Code..

JSON:

{
is_error: "false",
is_error_msg: "Video/Image List",
images: [
     "https://m.xsw88.com/allimgs/daicuo/20230912/6054.png",
     "https://m.xsw88.com/allimgs/daicuo/20230912/6055.png.jpg",
     "https://m.xsw88.com/allimgs/daicuo/20230912/6056.png.jpg",
     "https://m.xsw88.com/allimgs/daicuo/20230912/6057.png.jpg",
     "https://m.xsw88.com/allimgs/daicuo/20230912/6058.png.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...

推荐答案

这是在android的一个pretty的通病。位图加载到内存可能会引起 OutOfMemoryException异常

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

我建议你在后台使用一个lib如毕加索,此LIB加载图像,为您和调整他们一行code,以满足您的ImageView。

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);
 
精彩推荐
图片推荐