Java 如何将图像从firebase加载到导航材料抽屉图标
我无法将firebase中的图像添加到导航抽屉 我在创建导航抽屉后获取图像的url,因为从firebase获取url的异步方法在创建导航抽屉后调用获取url的方法,因此无法更新它 我在创建导航抽屉后获取图像的url,因为从firebase获取url的异步方法在创建导航抽屉后调用获取url的方法,因此无法更新它。我曾经尝试创建一个类和方法来实现这一点,但是在onCreate方法执行之后,它们会被调用,我正试图从中获取图像urlJava 如何将图像从firebase加载到导航材料抽屉图标,java,android,firebase,materialdrawer,Java,Android,Firebase,Materialdrawer,我无法将firebase中的图像添加到导航抽屉 我在创建导航抽屉后获取图像的url,因为从firebase获取url的异步方法在创建导航抽屉后调用获取url的方法,因此无法更新它 我在创建导航抽屉后获取图像的url,因为从firebase获取url的异步方法在创建导航抽屉后调用获取url的方法,因此无法更新它。我曾经尝试创建一个类和方法来实现这一点,但是在onCreate方法执行之后,它们会被调用,我正试图从中获取图像url if(profileImageUrlRef!=null) { prof
if(profileImageUrlRef!=null)
{
profileImageUrlRef.getDownloadUrl().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时的公共无效(Uri)
{
urlImage=uri.toString();
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
//处理任何错误
}
});
}
init(新的AbstractDrawerImageLoader(){
@凌驾
公共空集(ImageView、Uri、可绘制占位符、字符串标记){
Glide.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
}
@凌驾
公共作废取消(ImageView ImageView){
滑动。使用(客户端服务。此)。清除(图像视图);
}
@凌驾
公共可绘制占位符(上下文ctx、字符串标记){
//为不同的imageView目标定义不同的占位符
//默认标记可通过DroperImageLoader.tags访问
//可以通过字符串检查自定义的。请参阅CustomUrlBasePrimaryDrawerItem第111行
if(DrawerImageLoader.Tags.PROFILE.name().equals(tag)){
返回paureruutils.getPlaceHolder(ctx);
}else if(DrawerImageLoader.Tags.ACCOUNT_HEADER.name().equals(tag)){
返回新的IconicsDrawable(ctx).iconText(“”).backgroundColorRes(com.mikepenz.materialdrawer.R.color.primary).sizeDp(56);
}else if(“customUrlItem”.equals(标记)){
返回新的IconicsDrawable(ctx).iconText(“”).backgroundColorRes(R.color.md_red_500).sizeDp(56);
}
//我们使用默认的一个
//DrawerImageLoader.Tags.PROFILE\u DRAWER\u ITEM.name()
返回超级占位符(ctx,标签);
}
});
最终AccountHeader headerResult=新AccountHeaderBuilder()
.withActivity(本)
.带头部背景(右侧可拉深背景)
.addProfiles(
新的ProfileDrawerItem()。带有名称(“Ceo AtRalk”)。带有电子邮件(“info@atralk.co.zam“”。带有图标(urlImage)
)
.withOnAccountHeaderListener(新的AccountHeader.OnAccountHeaderListener(){
@凌驾
公共布尔OnProfile已更改(视图、IProfile配置文件、布尔currentProfile){
返回false;
}
})
.build();
我希望url image变量在传递给withIcon方法时使用url填充我曾经编写了一个
异步任务
,将配置文件映像缓存到SD卡:
/**
* AsyncTask: Profile Image
* @author Martin Zeitler
**/
public class ProfileImageTask extends AsyncTask<String, Void, Uri> {
private static final String LOG_TAG = ProfileImageTask.class.getSimpleName();
private static final boolean mDebug = BuildConfig.DEBUG;
private String fileName = "photo.jpg";
private boolean enforce = false;
private String sdPath;
private String url;
/** {@link IProfileImageTask} listener */
private IProfileImageTask listener;
/** Constructor */
@RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public ProfileImageTask(@NonNull Context context, @NonNull String url, @NonNull IProfileImageTask listener) {
this.sdPath = context.getExternalFilesDir(context.getResources().getString(R.string.app_name)) + "/";
this.listener = listener;
this.url = url;
}
@Override
protected void onPreExecute() {
/* setup destination directory */
File directory = new File(this.sdPath + Constants.FILESYSTEM_DIRECTORY_IMAGES);
if (! directory.exists()) {
//noinspection ResultOfMethodCallIgnored
directory.mkdirs();
}
/* setup file name */
String[] parts = this.url.split("/");
this.fileName = parts[parts.length - 1];
}
@Override
protected Uri doInBackground(String... arguments) {
File file = new File(this.sdPath + Constants.FILESYSTEM_DIRECTORY_IMAGES, this.fileName);
if(file.exists() && this.enforce) {
if(mDebug) {
Log.d(LOG_TAG, "delete profile image: " + file.getPath() + ", size: " + file.length() + " bytes.");
}
//noinspection ResultOfMethodCallIgnored
file.delete();
}
if (! file.exists()) {
try {
URLConnection conn = new URL(this.url).openConnection();
conn.connect();
InputStream in = conn.getInputStream();
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024]; int c;
while ((c = in.read(b)) != -1) {out.write(b, 0, c);}
out.close();
in.close();
} catch (IOException e) {
if(mDebug) {Log.e(LOG_TAG, "" + e.getMessage());}
} finally {
if(mDebug) {
Log.d(LOG_TAG, "new cached profile image: " + file.getPath() + ", size: " + file.length() + " bytes.");
}
}
} else if(mDebug) {
Log.d(LOG_TAG, "existing profile image: " + file.getPath() + ", size: " + file.length() + " bytes.");
}
return Uri.fromFile(file);
}
@Override
protected void onPostExecute(Uri uri) {
if (listener != null && uri != null) {
this.listener.OnImageAvailable(uri);
}
}
}
这是回调接口
,用于在任务完成时设置图像:
public interface IProfileImageTask {
/**
* indicates that the operation has finished.
* @param localUri
**/
void OnImageAvailable(@NonNull Uri localUri);
}
它的实施:
@Override
public void OnImageAvailable(@NonNull Uri uri) {
/* the Main {@link DrawerLayout} */
this.mDrawerLayout = this.findViewById(resIdLayout);
if (! this.mDrawerLayout.isInEditMode()) {
/* the {@link NavigationView} Drawer Menu */
this.mNavigationDrawer = this.findViewById(resIdDrawer);
/* the {@link NavigationView} Drawer Header */
View header = this.mNavigationDrawer.getHeaderView(0);
AppCompatImageView photoUrl = header.findViewById(R.id.photoUrl);
/* setting the photo-url */
if (this.currentUser != null && this.currentUser.getPhotoUrl() != null) {
photoUrl.setImageURI(uri);
}
}
}
Constants.FILESYSTEM\u目录\u映像我收到一个未解决的错误。我要进口什么才能让它消失呢。Im使用firebase和currentUser.getPhotoUrl不再工作。
@Override
public void OnImageAvailable(@NonNull Uri uri) {
/* the Main {@link DrawerLayout} */
this.mDrawerLayout = this.findViewById(resIdLayout);
if (! this.mDrawerLayout.isInEditMode()) {
/* the {@link NavigationView} Drawer Menu */
this.mNavigationDrawer = this.findViewById(resIdDrawer);
/* the {@link NavigationView} Drawer Header */
View header = this.mNavigationDrawer.getHeaderView(0);
AppCompatImageView photoUrl = header.findViewById(R.id.photoUrl);
/* setting the photo-url */
if (this.currentUser != null && this.currentUser.getPhotoUrl() != null) {
photoUrl.setImageURI(uri);
}
}
}