2012-10-26 53 views
1

我在我的互聯網上顯示此圖像時出現問題。 我不知道如何使它工作。 我是新來的android。使用setImageBitmap從圖像網站AsyncTask ImageView

的問題是,部分...

imView = (ImageView) findViewById(R.id.imageView1); 
imView.setImageBitmap(bm); //error 

謝謝。

我的代碼

public class CarregaImagem extends AsyncTask<String, Void, String>{ 
    String imageUrl = "http://www.cuboweb.com.br/android/images/logoconsulfarma.png"; 
    private ProgressDialog progress; 
    private Activity activity; 
    Bitmap bmImg; 

    public CarregaImagem(Activity activity){ 
     this.activity = activity; 
    } 

    protected void onPreExecute() { 
     progress = new ProgressDialog(activity); 
     progress.setTitle("Aguarde..."); 
     progress.setMessage("Carregando..."); 
     progress.show();  
    } 

    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     try { 
      URL aURL = new URL(imageUrl); 
      final URLConnection conn = aURL.openConnection(); 
      conn.connect(); 
      final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
      final Bitmap bm = BitmapFactory.decodeStream(bis); 
      BitmapFactory.decodeStream(new URL(imageUrl).openConnection().getInputStream()); 
      bis.close(); 
     } catch (IOException e) { 
      imageUrl = ""; 
     } catch(Exception f){ 
      imageUrl = ""; 
     } 
     return imageUrl;   
    } 

    protected void onPostExecute(String imageUrl) { 

     if(!imageUrl.equals("")){ 
      imView = (ImageView) findViewById(R.id.imageView1); 
      imView.setImageBitmap(bm); //error 
     } else{ 
      Toast.makeText(activity, "Não foi possível obter resultados", Toast.LENGTH_LONG).show(); 
     }   
     progress.dismiss();   
    } 
} 
+0

第二行給你什麼錯誤? – itsbruce

+0

@Flip_novidade請看我的回答,它會解決你的問題 –

+0

正確...完美...謝謝 –

回答

2

您創建doInBackground位圖,你永遠不會使用。 返回位圖並在onPostExecute中使用它。

+0

怎麼樣? 可以顯示我嗎? –

+0

in doInBackground: return BitmapFactory.decodeStream(new URL(url).openConnection()。getInputStream()); in onPostExecute: imView.setImageBitmap(result); –

+0

@olivier將代碼添加到您的答案,編輯它。 –

1

請嘗試以下代碼以從網站下載圖像並在imageview中顯示。

public class MainActivity extends Activity { 

    ImageView mImgView1; 
    static Bitmap bm; 
    ProgressDialog pd; 
    String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg"; 
    BitmapFactory.Options bmOptions; 

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

     mImgView1 = (ImageView) findViewById(R.id.mImgView1); 
     pd = ProgressDialog.show(MainActivity.this, "Aguarde...", 
       "Carregando..."); 
     new ImageDownload().execute(""); 
    } 

    public class ImageDownload extends AsyncTask<String, Void, String> { 

     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      bmOptions = new BitmapFactory.Options(); 
      bmOptions.inSampleSize = 1; 
      loadBitmap(imageUrl, bmOptions); 
      return imageUrl; 
     } 

     protected void onPostExecute(String imageUrl) { 
      pd.dismiss(); 
      if (!imageUrl.equals("")) { 
       mImgView1.setImageBitmap(bm); 
      } else { 
       Toast.makeText(MainActivity.this, 
         "Não foi possível obter resultados", Toast.LENGTH_LONG) 
         .show(); 
      } 
     } 

    } 

    public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) { 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bm = BitmapFactory.decodeStream(in, null, options); 
      in.close(); 
     } catch (IOException e1) { 
     } 
     return bm; 
    } 

    private static InputStream OpenHttpConnection(String strURL) 
      throws IOException { 
     InputStream inputStream = null; 
     URL url = new URL(strURL); 
     URLConnection conn = url.openConnection(); 

     try { 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       inputStream = httpConn.getInputStream(); 
      } 
     } catch (Exception ex) { 
     } 
     return inputStream; 
    } 
} 
+0

我應該在清單中擁有什麼權限? –

+0

瞭解它 非常感謝 完美... 下一步是使網站圖像的畫廊 –

+0

@Flip_novidade如果我的回答是幫助你,那麼請接受它。 –