2017-07-29 86 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看到我更新的答案先生。 –