2011-12-07 101 views
5

我已經看到了這個問題:android how to download an 1mb image file and set to ImageView
它並沒有解決我的問題,因爲它只是說明如何顯示位圖你已經擁有它。下載圖像在Android

我正嘗試從URL下載圖像,使其與Android設備上的ImageView一起顯示。我不知道如何做到這一點。

我已經在互聯網上有點看,這是我到目前爲止的代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Set local image 
    ImageView image = (ImageView) findViewById(R.id.test_image); 
    image.setImageResource(R.drawable.test2); 

    //Prepare to download image 
    URL url;   
    InputStream in; 

    //BufferedInputStream buf; 
    try { 
     url = new URL("http://i.imgur.com/CQzlM.jpg"); 
     in = url.openStream(); 

     out = new BufferedOutputStream(new FileOutputStream("testImage.jpg")); 
     int i; 

     while ((i = in.read()) != -1) { 
      out.write(i); 
     } 
     out.close(); 
     in.close(); 

     buf = new BufferedInputStream(in); 
     Bitmap bMap = BitmapFactory.decodeStream(buf); 
     image.setImageBitmap(bMap); 
     if (in != null) { 
     in.close(); 
     } 
     if (buf != null) { 
     buf.close(); 
     } 
    } catch (Exception e) { 
     Log.e("Error reading file", e.toString()); 
    } 
} 
+0

如果你找到答案,你應該接受它。這樣,其他用戶知道它的工作原理。祝你好運! – Entreco

回答

10

的代碼可能會工作,alltough你在你的主線程下載你的形象。這意味着,當它需要超過5秒的下載,你會看到着名的ANR對話框,你的應用程序將崩潰...

你應該在後臺線程中下載你的圖像,並將結果發回到你的主線程。回到主線程中,您可以使用下載的圖像更新您的圖像視圖。

下面是一個例子:

package nl.entreco.stackoverflow; 

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 StackOverflowActivity 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://i.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(); 

      /* 
      * THIS IS NOT NEEDED 
      * 
      * YOU TRY TO CREATE AN ACTUAL IMAGE HERE, BY WRITING 
      * TO A NEW FILE 
      * YOU ONLY NEED TO READ THE INPUTSTREAM 
      * AND CONVERT THAT TO A BITMAP 
      out = new BufferedOutputStream(new FileOutputStream("testImage.jpg")); 
      int i; 

      while ((i = in.read()) != -1) { 
       out.write(i); 
      } 
      out.close(); 
      in.close(); 
      */ 

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

} 

} 

不要忘記將permission.INTERNET添加到您的清單,如果沒有,你會得到一個錯誤......

+0

我試過了。它不提供任何錯誤,但圖像不會加載。 Internet權限**存在於AndroidManifest.xml文件中。這可能與正在其他類中的DownloadImage類有關嗎? – Koen027

+0

沒關係,我改變了一下代碼,它工作。在函數setImage中,我將唯一一行更改爲:'mImageView.setImageDrawable(drawable);' – Koen027

+0

Yar,抱歉等待。測試完成後忘記這一點。 – Koen027