从图像URL下载图像到图像视图

问题描述:

我想从imageurl下载图像该图像来自该网址是一个高分辨率image.it工作正常在所有高分辨率手机,当我试图加载到一个图像它正在抛出内存泄漏异常的mdpi模拟器。从图像URL下载图像到图像视图

我如何处理这种情况,我想在每个这一形象和每一个屏幕,所以我声明位图作为一个全局变量

有没有什么办法,以减少图像的大小,同时下载。使用IAM下面的代码下载的图像

C1是参考图像视图

bitmap = BitmapFactory.decodeStream((InputStream)new URL(logourltop.get(0)).getContent()); 
cl.setImageBitmap(bitmap) ; 

(或)

最好是使用urlimagehelper项目下载的图像时需要

UrlImageViewHelper.setUrlDrawable(cl,logourltop.get(0)); 

还有一个疑问是我正在改变在同一活动中的意见,通过使用

setContentView(R.layout.filename). 

如果我更改列表项的视图中单击分配给位图的内存将被释放或没有。

可以请你建议我到一个更好的办法(分配的对象和位图该视图的内存)避免内存泄漏。

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.net.URL; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ImageView; 

public class downloadimg extends Activity { 

// 
private ImageView mImageView; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Find the reference to the ImageView 
    mImageView = (ImageView) findViewById(R.id.test_image); 

    // You can set a temporary background here 
    //image.setImageResource(null); 

    // Start the DownloadImage task with the given url 
    new DownloadImage().execute("http://demo.imgur.com/CQzlM.jpg"); 
} 


/** 
* Simple functin to set a Drawable to the image View 
* @param drawable 
*/ 
private void setImage(Drawable drawable) 
{ 
    mImageView.setBackgroundDrawable(drawable); 
} 

public class DownloadImage extends AsyncTask<String, Integer, Drawable> { 

    @Override 
    protected Drawable doInBackground(String... arg0) { 
     // This is done in a background thread 
     return downloadImage(arg0[0]); 
    } 

    /** 
    * Called after the image has been downloaded 
    * -> this calls a function on the main thread again 
    */ 
    protected void onPostExecute(Drawable image) 
    { 
     setImage(image); 
    } 


    /** 
    * Actually download the Image from the _url 
    * @param _url 
    * @return 
    */ 
    private Drawable downloadImage(String _url) 
    { 
     //Prepare to download image 
     URL url;   
     BufferedOutputStream out; 
     InputStream in; 
     BufferedInputStream buf; 

     //BufferedInputStream buf; 
     try { 
      url = new URL(_url); 
      in = url.openStream(); 



      // Read the inputstream 
      buf = new BufferedInputStream(in); 

      // Convert the BufferedInputStream to a Bitmap 
      Bitmap bMap = BitmapFactory.decodeStream(buf); 
      if (in != null) { 
       in.close(); 
      } 
      if (buf != null) { 
       buf.close(); 
      } 

      return new BitmapDrawable(bMap); 

     } catch (Exception e) { 
      Log.e("Error reading file", e.toString()); 
     } 

     return null; 
    } 

} 

} 
+0

-1码只能回答 – WarrenFaith 2013-03-22 12:17:19

+1

代码不打烊的资源安全...关闭finally块上的资源。 – 2013-03-22 12:20:33

基本上,你必须下载图像数据,并在流或字节数组形式的图像数据,可以使用从BitmapFactory的方便的方法,会给你一个位图出来,并与此位图在手中,您可以将其直接设置为ImageView。

请确保您在网络中以单独的线程执行下载,例如,使用AsyncTask .doInBackground()并在UI线程上的ImageView上设置位图。通过将其设置为AsyncTask的方法onPostExecute()或致电Activity.runOnUIThread()

+0

但如果位图具有高分辨率图像我正在越来越内存泄漏错误如何根据设备堆大小调整图像 – 2013-03-22 12:28:00

+0

这就是为什么他们发明了BitampFactory.Options:https://developer.android.com/reference/android/图形/ BitmapFactory.Options.html与它一起玩,找到最合适的。在BitmapFactory上,您传递字节数组或流以获取位图,您可以提供Options对象作为参数,这将应用于防止内存不足错误......在这种情况下,您肯定希望使用流而不是字节数组。 – 2013-03-22 12:29:20

+0

毕竟有效吗? – 2013-03-22 16:26:48

希望这将有助于...

public class BitmapResizer { 

    public static Bitmap decodeFile(File f,int requiredSize){ 
    try { 
     //decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE=requiredSize; 
     int width_tmp=o.outWidth, height_tmp=o.outHeight; 
     int scale=1; 
     while(true){ 
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
       break; 
      width_tmp/=2; 
      height_tmp/=2; 
      scale*=2; 
     } 

     //decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) { 

    } 
    return null; 
}} 
+0

下一个代码只回答下一个投票...“希望这会有所帮助......” – WarrenFaith 2013-03-22 13:22:27

+0

用户只需要答案[email protected] WarrenFaith – 2013-03-23 06:48:28

+0

和所有其他寻找答案的人只是想复制和粘贴代码而不学习任何东西......真正的真实,支持懒惰一次... – WarrenFaith 2013-03-23 10:30:18