gpt4 book ai didi

android - 内存不足异常 : Load Bunch Of Images From Server

转载 作者:行者123 更新时间:2023-11-29 17:48:02 25 4
gpt4 key购买 nike

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

我已经创建了用于显示图像的 GridView,它正在成功运行....

但问题是,当图像数量增加时,由于堆内存增加,它会抛出 OutOfMemoryException...

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

所以,我的问题是:如何从服务器成功加载所有图像而不会出现此问题......我应该做些什么改变来解决这个问题

这是我的 JSON 和代码..

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);
}
}
}
}
}

请告诉我应该做哪些改变来解决我的问题..

提前感谢您的帮助...

最佳答案

这是android中一个很常见的问题。将位图加载到内存中可能会导致 OutOfMemoryException

我建议你使用类似 picasso 的库,这个库在后台为您加载图像,并通过一行代码调整它们的大小以适合您的 ImageView。

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

关于android - 内存不足异常 : Load Bunch Of Images From Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25077177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com