2015-12-21 67 views
0

我正在加載來自Twitter供稿的位圖。我使用下面的代碼Bitmap notloading in Imageview

private class getBitmapFromLink extends AsyncTask<String, Void, Bitmap> { 
    private ImageView imgView; 

    public getBitmapFromLink(ImageView imgView) { 
     this.imgView = imgView; 
    } 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     Bitmap myBitmap; 
     try { 
      URL url = new URL(params[0]); 
      HttpURLConnection connection = (HttpURLConnection) url 
        .openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } catch (IOException e) { 
      Log.v("BITMAP", e.getMessage()); 
      return null; 
     } catch (Exception e) { 
      Log.v("BITMAP", e.getMessage()); 
      return null; 
     } 

    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     imgView.setImageBitmap(result); 
     imgView.invalidate(); 
    } 

} 

當調試myBitmap = BitmapFactory.decodeStream(input);線直接來到catch塊,並在返回零線。在調試時,我嘗試在日誌上打印錯誤消息,但它不執行該行並直接返回語句。

由於提前

+0

是網址有效嗎?你爲什麼不使用畢加索或壁畫來處理圖片? – Blackbelt

+0

是的。 url有效 - http://pbs.twimg.com/media/CWgUjyHUAAA7eZ3.jpg –

+0

使用picasso從url加載圖片,你可以在這裏查看示例http://stackoverflow.com/questions/20181491/use-picasso-to -get-a-callback-with-a-bitmap –

回答

0

可以使用通用圖像裝載機,比

imageLoader = ImageLoader.getInstance(); // Get singleton instance 
    ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(ProfileScreen.this)); 
    imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance(); 
    imageOptions = new DisplayImageOptions.Builder() 
      .showImageOnFail(R.drawable.user_icon) 
      .showImageOnLoading(R.drawable.user_icon) 
      .showImageForEmptyUri(R.drawable.user_icon) 
      .cacheInMemory(true) 
      .resetViewBeforeLoading(true) 
      .build(); 

而且比你在哪裏得到的URL中使用這些代碼行

 iimageLoader.displayImage("your url", imgView, imageOptions); 

          imageLoader.loadImage("your url", new SimpleImageLoadingListener() { 
           @Override 
           public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
            // Do whatever you want with Bitmap 
            imgView.setImageBitmap(loadedImage); 

           } 
          }); 

你可以還可以使用Picasso庫來執行您的操作...爲了您可以使用這些代碼行爲它

Picasso.with(getContext()).load("your url").into(new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        //do what ever you want with your bitmap 
        imgView.setImageBitmap(loadedImage); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 

       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 

       } 
      }); 
+0

@ unflagged.destination ...這些解決方案對你是否有用......如果這對你有所幫助,請直接投票或接受 –