如何在Android中从DroperItem中的uri加载图标/化身
我正在为我的项目使用heinrichreimersoftware的材料抽屉库。要从抽屉项目中的URL加载图像,我使用glide如何在Android中从DroperItem中的uri加载图标/化身,android,navigation-drawer,Android,Navigation Drawer,我正在为我的项目使用heinrichreimersoftware的材料抽屉库。要从抽屉项目中的URL加载图像,我使用glide final ImageView image = new ImageView(this); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { Looper.
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))
);
final ImageView image=新的ImageView(此);
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
Looper.prepare();
试一试{
位图=滑动。
使用(MainActivity.this)。
加载(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl())。
asBitmap()。
进入(-1,-1)。
get();
}捕获(最终执行例外){
Log.e(标记,e.getMessage());
}捕获(最终中断异常e){
Log.e(标记,e.getMessage());
}
返回null;
}
@凌驾
受保护的void onPostExecute(void dummy){
if(null!=位图){
//完整的位图应该在这里可用
设置图像位图(位图);
Log.d(标签,“图像加载”);
};
}
}.execute();
出票人.附加项(新出票人)
.setImage(这是位图)
.setTextPrimary(getString(R.string.profile))
);
但是它没有加载图像,任何帮助都将不胜感激使用下面的代码在ImageView中加载图像
Glide.with(context)
.load(FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl())
.into(imageView);
首先,不需要使用异步任务。如果要设置位图,请使用以下代码
// 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);
}
});
现在,正如你所说,你想在抽屉菜单中使用它,在这种情况下,你可以这样做
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))
);
看到我的答案了,先生,我希望这能帮助你。好的,正如你所看到的,我想把这个位图对象设置为抽屉项目的图标。那么,如何使用上述代码处理抽屉项?@neerajgiri当onResourceReady方法调用时,您可以将位图保存在其中,并使用该位图可以在任何imageView中进行设置。或者,如果您不想这样做,则直接在onResourceReady中设置抽屉项目imageview,它将在imageview中设置您的图像。请查看此URL()并清除我的问题,请参阅从第101行开始的函数。我需要从URL加载图像,而不是从drawable加载。你能给我一些关于如何处理这个问题的建议吗?
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))
);