2011-05-04 82 views
0

我想從一個按鈕上的url加載圖片點擊&以在活動imageview上顯示它。Android從按鈕上點擊url加載圖片

該怎麼辦? 我嘗試下面的代碼,但它顯示了像java.net.UnknownHostException的系統錯誤主機已解決。

package com.v3.thread.fetchImage; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

import org.apache.http.HttpException; 
import org.apache.http.conn.HttpHostConnectException; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

public class MainThreadActivity extends Activity { 
    ImageView img_downloaded; 
    Button btn_download; 
    String fileurl = "http://variable3.com/files/images/email-sig.jpg"; 

Bitmap bmImg; 
private ProgressDialog dialog; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //new MainThreadActivity().onPreExecute(); 

    img_downloaded = (ImageView) findViewById(R.id.imageView1); 
    btn_download = (Button) findViewById(R.id.btnLoad); 

    btn_download.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      try { 
       downloadfile(fileurl); 
      } catch (HttpHostConnectException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (HttpException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 


public void AbstractProgressTask() { 
    dialog = new ProgressDialog(this); 
} 

protected void onPreExecute() {new MainThreadActivity().onPreExecute(); 
    this.dialog.setMessage("Loading. Please wait..."); 
    this.dialog.show(); 
} 

/** 
* method is called after the work is done. 
* 
* @param success result of #doInBackground method. 
*/ 

protected void onPostExecute(final Boolean success) { 
    if (dialog.isShowing()) { 
     dialog.dismiss(); 
    } 
} 
// automatically done on worker thread (separate from UI thread) 
protected Boolean doInBackground(final String... args) { 

    // here is your code 
    return true; 
} 

void downloadfile(String fileurl) throws HttpException,HttpHostConnectException { 
    URL myFileUrl = null; 
    try { 
     myFileUrl = new URL(fileurl); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
     conn.setDoInput(true); 
     conn.connect(); 
     int length = conn.getContentLength(); 
     if(length>0) 
     { 
      int[] bitmapData =new int[length]; 
      byte[] bitmapData2 =new byte[length]; 
      InputStream is = conn.getInputStream(); 
      bmImg = BitmapFactory.decodeStream(is); 

      //img.setImageBitmap(bmImg); 
     } 
     else 
     { 
      int[] bitmapData =new int[length]; 
      byte[] bitmapData2 =new byte[length]; 
      InputStream is = conn.getInputStream(); 
      bmImg = BitmapFactory.decodeStream(is); 
     } 

    } 
    catch(IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
} 

請告訴我在哪裏我甲肝改變

+0

的可能重複[加載圖片從按鈕點擊url](http://stackoverflow.com/questions/5880191/load-an-image-from-url-on-button-click) – 2011-05-04 09:47:48

回答

1

這似乎是一個大量的代碼只是爲了顯示對一個ImageView的。試試這個:

URL url = new URL("http://variable3.com/files/images/email-sig.jpg"); 
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
img_downloaded.setImageBitmap(bmp); 
-1

只需撥打以下方法上的按鈕進行點擊:

Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException 
    { 
     return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name); 
    } 

其中URL是URL傳遞和src_name是任何字符串(比如 「SRC」)

希望這會幫助你

0

我想從一個按鈕上點擊網址加載圖片點擊&顯示它活動的ImageView

你要做的內onPageFinished類似如下:

public void onPageFinished (WebView view, String url) {  
    wv.loadUrl(javascript);   

    view.getSettings().setLoadsImagesAutomatically(true); 
    wv.setVisibility(View.VISIBLE); 
} 
+0

這只是一個答案。請在答案中多加一點努力。描述問題是什麼或你的代碼做了什麼。 – 2011-05-24 12:54:13

+0

我認爲人們發現他們爲什麼投票贊成有用。根據規則。 - 雷諾你在什麼基礎上責怪我不知道。 – RobinHood 2011-06-11 08:45:26

1

使用下面的代碼,它會幫助你

Drawable drawable = LoadImageFromWebOperations(backImgUrl); 
bagImgBtn.setBackgroundDrawable(drawable); 

private Drawable LoadImageFromWebOperations(String url) { 
    // TODO Auto-generated method stub 
    try 
    { 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
    }catch (Exception e) { 
     if (LogD) { 
      Log.d(TAG, e.toString()); 
      e.printStackTrace(); 
     } 
     return null;  
    } 
}