无法从Firebase存储在ImageView中显示图像
我有一个应用程序,允许用户将图像上传到Firebase存储桶,然后获取图像文件的下载URL并将其添加到Firebase数据库.URL的格式为:-https://firebasestorage.googleapis.com/v0/b/bucket_name.appspot.com/o/filename?alt=media&token=token
I have an app which allows the users to upload the images to a Firebase bucket,then I get the download URL of the image file and add it to a firebase database.The URL is in the form :- https://firebasestorage.googleapis.com/v0/b/bucket_name.appspot.com/o/filename?alt=media&token=token
然后我尝试使用Glide将图像加载到RecyclerView中,但无法这样做,并且它无法显示图像.
Then I try to load the images into an RecyclerView using Glide but I am unable to do so and It fails to display the image.
如何获取以.png或.jpeg等结尾的绝对图像URL,据此可以轻松地加载图像.据我所知,问题出在url
How can I get an absolute Image URL which ends in .png or .jpeg etc which can be easily used to load the image.As far as I know the problem is with the url
从相机获取图像
Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 3);
获取图像的文件路径
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FILE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
String path=String.valueOf(data.getData());
file = Uri.fromFile(new File(path));
创建文件元数据
metadata = new StorageMetadata.Builder()
.setContentType("image/jpeg")
.build();
将文件和元数据上传到路径"images/avator.jpg"
Upload file and metadata to the path 'images/avator.jpg'
uploadTask = storageRef.child("images/"+file.getLastPathSegment()).putFile(file, metadata);
收听状态更改,错误和上载完成.
Listen for state changes, errors, and completion of the upload.
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
}
});
要使用Glide库加载图像
To load the Image using Glide Library
Glide.with(getActivity())
.load(new File(downloadUrl)) // Uri of the picture
.into(Imageview);