2015-12-13 49 views
4

我使用ImageView的滑行得到位,我想從imageview--getDrawable()給出空對象,而試圖從ImageView的

ImageView imageView = (ImageView) findViewById(R.id.dp); 
Glide.with(this).load("http://graph.facebook.com/1615242245409408/picture?type=large").into(imageView); 
Bitmap fbbitmap2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); 

得到位,但它給

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference 

幫幫我。

回答

5

Glide異步加載圖像,所以在啓動加載操作之後,您對位圖的測試將返回null,因爲圖像尚未加載。

要知道,當你的形象確實已經加載,你可以在你的滑翔要求設置一個監聽器,例如:

Glide.with(this) 
    .load("http://graph.facebook.com/1615242245409408/picture?type=large") 
    .listener(new RequestListener<Uri, GlideDrawable>() { 
     @Override 
     public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) { 
      return false; 
     } 

     @Override 
     public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
      // drawable is in resource variable 
      // if you really need to access the bitmap, you could access it using ((GlideBitmapDrawable) resource).getBitmap() 
      return false; 
     } 
    }) 
    .into(imageView);