将图像表单android上传到Firebase后如何获取图像URL?
问题描述:
我正在制作一个用户将图像上传到Firebase存储上的应用程序.上载图像后,我想将图像的URL和其他详细信息上载到我自己的API.我如何获取用户刚刚上传的图像的Uri. 此教程教了如何做上传,但未显示如何获取图片网址.我尝试了所有教程,但没有一个显示我想要的东西.
I am making an App that user uploaded images on Firebase storage. After Uploading the image i want to upload the Images's URL and other details to my own API. How can i get the Uri of that image which the user just uploaded. This tutorial teaches how to do the uploading but doesn't show how to get the image URL. I tried all the tutorials but none shows the thing that i want.
答
根据文档,则可以在.onSuccessListener
中调用.getDownloadUrl
来获取图像URL.
According to the documentation, you can call .getDownloadUrl
in the .onSuccessListener
to get the image URL.
以下是文档中的示例:
// Get the data from an ImageView as bytes
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.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) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});