
我正在开发一个从Server 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这样的lib,这个lib在后台为你加载图像并调整它们以适应你的ImageVIEw一行代码.
Picasso.with(context).load(URL).fit().centerCrop().into(imageVIEw); 总结 以上是内存溢出为你收集整理的android – OutOfMemoryException:从服务器加载一堆图像全部内容,希望文章能够帮你解决android – OutOfMemoryException:从服务器加载一堆图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)