2017-07-29 46 views
0

我正在爲我的項目使用heinrichreimersoftware'Material抽屜庫。要從Drawer項目中的URL加載圖像,我正在使用滑動。如何從uri中的Draweritem中的uri加載圖標/頭像

final ImageView image = new ImageView(this); 

    new AsyncTask<Void, Void, Void>() { 
     @Override 
     protected Void doInBackground(Void... params) { 
      Looper.prepare(); 
      try { 
       theBitmap = Glide. 
         with(MainActivity.this). 
         load(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl()). 
         asBitmap(). 
         into(-1,-1). 
         get(); 
      } catch (final ExecutionException e) { 
       Log.e(TAG, e.getMessage()); 
      } catch (final InterruptedException e) { 
       Log.e(TAG, e.getMessage()); 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void dummy) { 
      if (null != theBitmap) { 
       // The full bitmap should be available here 
       image.setImageBitmap(theBitmap); 
       Log.d(TAG, "Image loaded"); 
      }; 
     } 
    }.execute(); 




    drawer.addItem(new DrawerItem() 
      .setImage(this,theBitmap) 
      .setTextPrimary(getString(R.string.profile)) 

    ); 

但它不加載圖像,任何幫助將不勝感激以下爲ImageView的圖像加載代碼

+0

看到我的回答先生我希望這會幫助你。 –

回答

0

使用;

Glide.with(context) 
.load(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl()) 
.into(imageView); 
1

首先不需要使用異步任務。如果要設置位圖,請使用下面的代碼。

// If you want to save bitmap in bitmap object the use this object. 
Bitmap theBitmap; 

Glide.with(context) 
      .load("Your URL") 
      .asBitmap() 
      .into(new SimpleTarget<Bitmap>() 
      { 
       @Override 
       public void onResourceReady(Bitmap res, GlideAnimation<? super Bitmap> animation) 
       { 
        // assign res(Bitmap object) to your local theBitmap(Bitmap object) 
        theBitmap = res; 
        // Set bitmap to your imageview 
        yourImageView.setImageBitmap(res); 
       } 
      }); 

如果您只是直接將圖像設置爲您的ImageView然後按照此。

Glide.with(context) 
.load("Your URL") 
.placeholder(R.drawable.your_stub_image) 
.into(yourImageView); 

現在當你說你想在抽屜菜單中使用它時,你可以這樣做。

Drawable myDrawable = new BitmapDrawable(getResources(), theBitmap); 
drawer.addItem(new DrawerItem() 
        .setRoundedImage(myDrawable) 
        .setTextPrimary(getString(R.string.lorem_ipsum_short)) 
        .setTextSecondary(getString(R.string.lorem_ipsum_long)) 
); 
+0

好的,你可以看到我想將這個位圖對象設置爲抽屜項目的圖標。所以我如何使用上面的代碼來處理抽屜項目? –

+0

@neerajgiri當onResourceReady方法調用時,您可以將位圖保存在該位圖中並使用該位圖,您可以在任何imageView中設置它。或者,如果你不想這樣做,那麼直接在你的圖像視圖中設置你的抽屜項目imageview onResourceReady它會設置你的圖像。 –

+0

@neerajgiri看到我更新的答案先生。 –