2012-02-25 82 views
0
public class ImageDownloader extends AsyncTask<String, Void, Drawable> { 
Context context; 
ImageView image; 
public ImageDownloader(ImageView image) { 
    this.image = image; 
} 
protected void onPreExecute() { 
} 

protected Drawable doInBackground(String... urls) { 
    InputStream is; 
    Drawable d = null ; 
    try { 
     is = (InputStream)new URL(urls[0]).getContent(); 
     d = Drawable.createFromStream(is, "Image"); 
     return d; 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return d; 
} 
protected void onPostExecute(Drawable d) { 
    if(d != null){ 
     d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
     image.setBackgroundDrawable(d); 
    }else{ 
     image.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.noimage)); 
     Toast.makeText(context, "No image", Toast.LENGTH_LONG).show(); 
    } 

} 

}有没有另一种方式来显示来自url的图像?

,因为它的工作在开始的时候,但是当我下载了很多的图像会崩溃。我看着我的LogCat,这是因为没有更多的分配。 我想做一个像Pulse News这样的应用程序,它可以显示图像,但不会每次都崩溃。我在冰淇淋三明治,以防万一。

+0

http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – 2012-02-25 12:28:55

+0

但我不想下载图像只显示它们。 – Tsunaze 2012-02-25 12:34:46

+1

如何在不首先下载图片的情况下显示图片?当你在网上查看一些图像时,隐含的事情是你必须先下载图像。这不仅是图像,而且是网络上的另一个内容。 – hqt 2012-02-25 12:40:11

回答

0

您需要了解java中的SoftReference类。 继续前进并搜索它。你会得到你的答案

0

我发现这个类,它工作正常,非常好!

public class LoaderImageView extends LinearLayout{ 

private static final int COMPLETE = 0; 
private static final int FAILED = 1; 

private Context mContext; 
private Drawable mDrawable; 
private ProgressBar mSpinner; 
private ImageView mImage; 

/** 
* This is used when creating the view in XML 
* To have an image load in XML use the tag 'image="http://developer.android.com/images/dialog_buttons.png"' 
* Replacing the url with your desired image 
* Once you have instantiated the XML view you can call 
* setImageDrawable(url) to change the image 
* @param context 
* @param attrSet 
*/ 
public LoaderImageView(final Context context, final AttributeSet attrSet) { 
    super(context, attrSet); 
    final String url = attrSet.getAttributeValue(null, "image"); 
    if(url != null){ 
     instantiate(context, url); 
    } else { 
     instantiate(context, null); 
    } 
} 

/** 
* This is used when creating the view programatically 
* Once you have instantiated the view you can call 
* setImageDrawable(url) to change the image 
* @param context the Activity context 
* @param imageUrl the Image URL you wish to load 
*/ 
public LoaderImageView(final Context context, final String imageUrl) { 
    super(context); 
    instantiate(context, imageUrl);  
} 

/** 
* First time loading of the LoaderImageView 
* Sets up the LayoutParams of the view, you can change these to 
* get the required effects you want 
*/ 
private void instantiate(final Context context, final String imageUrl) { 
    mContext = context; 

    mImage = new ImageView(mContext); 
    mImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    mSpinner = new ProgressBar(mContext); 
    mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    mSpinner.setIndeterminate(true); 

    addView(mSpinner); 
    addView(mImage); 

    if(imageUrl != null){ 
     setImageDrawable(imageUrl); 
    } 
} 

/** 
* Set's the view's drawable, this uses the internet to retrieve the image 
* don't forget to add the correct permissions to your manifest 
* @param imageUrl the url of the image you wish to load 
*/ 
public void setImageDrawable(final String imageUrl) { 
    mDrawable = null; 
    mSpinner.setVisibility(View.VISIBLE); 
    mImage.setVisibility(View.GONE); 
    new Thread(){ 
     public void run() { 
      try { 
       mDrawable = getDrawableFromUrl(imageUrl); 
       imageLoadedHandler.sendEmptyMessage(COMPLETE); 
      } catch (MalformedURLException e) { 
       imageLoadedHandler.sendEmptyMessage(FAILED); 
      } catch (IOException e) { 
       imageLoadedHandler.sendEmptyMessage(FAILED); 
      } 
     }; 
    }.start(); 
} 

/** 
* Callback that is received once the image has been downloaded 
*/ 
Handler.Callback call = new Handler.Callback() { 
    public boolean handleMessage(Message msg) { 
     switch (msg.what) { 
     case COMPLETE: 
      mImage.setImageDrawable(mDrawable); 
      mImage.setVisibility(View.VISIBLE); 
      mSpinner.setVisibility(View.GONE); 
      break; 
     case FAILED: 
     default: 
      // Could change image here to a 'failed' image 
      // otherwise will just keep on spinning 
      break; 
     } 
     return true; 
    } 
}; 
private final Handler imageLoadedHandler =new Handler(call); 


/** 
* Pass in an image url to get a drawable object 
* @return a drawable object 
* @throws IOException 
* @throws MalformedURLException 
*/ 
private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException { 
    return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), "name"); 
} 

} 

要使用它,它很简单:

LoderImageView image = (LoaderImageView) findViewById(R.id.myid); 
image.setImageDrawable(url); 
相关问题