Firebase 存储,无法下载 url 成功。 (Android)
Firebase storage, cannot get download url successfully. (Android)
到目前为止,这是我的代码。
public void getimage() {
FirebaseStorage storagi = FirebaseStorage.getInstance();
StorageReference imagerefi = storagi.getReference();
userimage = (ImageView) findViewById(R.id.imageViewuser);
StorageReference pathReference = imagerefi.child("images/"+somename);
imagerefi.child("images/"+somename).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
uri2 = uri.toString();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);
}
我试过没有成功侦听器和许多其他东西。当我简单地从 firebase 控制台复制粘贴 url 时,我得到的图像完美运行。但是 uri2 没有按预期工作。
我基本上想获取以 firebase 用户 uid 命名的文件,该文件位于 firebase 存储根目录中名为 images 的文件夹中,并将其显示在 imageview 上。
非常感谢您的帮助!我已经尝试了数周来解决这个问题。
//最近从 stactoverflow 修改了一些代码 post //因此在当前配置中什么都不做,我知道
我希望 somename 是一个有效的名字。试试这个
只需在 onSuccess 内移动在 imageview 中设置图像的行,以确保 uri2 是来自 firebaseStorage
的 url
public void getimage() {
FirebaseStorage storagi = FirebaseStorage.getInstance();
StorageReference imagerefi = storagi.getReference();
userimage = (ImageView) findViewById(R.id.imageViewuser);
StorageReference pathReference = imagerefi.child("images/"+somename);
imagerefi.child("images/"+somename).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
uri2 = uri.toString();
Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Log.e("ERRORLOADING",exception.getMessage());
}
});
}
要解决这个问题,只需移动以下代码行:
Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);
在以下代码行之后的回调中:
uri2 = uri.toString();
请记住,onSuccess()
方法具有异步行为,如果您尝试在此方法之外使用 uri2
变量的值,它将始终是 null
,因为到时候您正在尝试访问它,它的值尚不可用。如果你想在该方法之外使用它,我建议你从这个 in which I have explained how it can be done using a custom callback. You can also take a look at this video 中看到我的答案的最后一部分以便更好地理解。
到目前为止,这是我的代码。
public void getimage() {
FirebaseStorage storagi = FirebaseStorage.getInstance();
StorageReference imagerefi = storagi.getReference();
userimage = (ImageView) findViewById(R.id.imageViewuser);
StorageReference pathReference = imagerefi.child("images/"+somename);
imagerefi.child("images/"+somename).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
uri2 = uri.toString();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);
}
我试过没有成功侦听器和许多其他东西。当我简单地从 firebase 控制台复制粘贴 url 时,我得到的图像完美运行。但是 uri2 没有按预期工作。
我基本上想获取以 firebase 用户 uid 命名的文件,该文件位于 firebase 存储根目录中名为 images 的文件夹中,并将其显示在 imageview 上。
非常感谢您的帮助!我已经尝试了数周来解决这个问题。
//最近从 stactoverflow 修改了一些代码 post //因此在当前配置中什么都不做,我知道
我希望 somename 是一个有效的名字。试试这个
只需在 onSuccess 内移动在 imageview 中设置图像的行,以确保 uri2 是来自 firebaseStorage
的 urlpublic void getimage() {
FirebaseStorage storagi = FirebaseStorage.getInstance();
StorageReference imagerefi = storagi.getReference();
userimage = (ImageView) findViewById(R.id.imageViewuser);
StorageReference pathReference = imagerefi.child("images/"+somename);
imagerefi.child("images/"+somename).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
uri2 = uri.toString();
Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Log.e("ERRORLOADING",exception.getMessage());
}
});
}
要解决这个问题,只需移动以下代码行:
Glide.with(this).asDrawable().apply(fitCenterTransform()).load(uri2).into(userimage);
在以下代码行之后的回调中:
uri2 = uri.toString();
请记住,onSuccess()
方法具有异步行为,如果您尝试在此方法之外使用 uri2
变量的值,它将始终是 null
,因为到时候您正在尝试访问它,它的值尚不可用。如果你想在该方法之外使用它,我建议你从这个