2017-07-29 11 views
0

私のプロジェクトには、引き出しライブラリheinrichreimersoftware'Material drawer libraryを使用しています。引き出しアイテムのURLから画像を読み込むには、私は滑空を使用しています。Androidの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で設定できます。または、それをしたくない場合は、引き出しアイテムのイメージビューをonResourceReadyの中に直接設定してください。これによりイメージビューにイメージが設定されます。 –

+0

@neerajgiri私の更新された回答を参照してください。 –

関連する問題