如何从Firebase获取URL图像并将其保存在字符串中?

时间:2019-03-27 23:55:31

标签: android

如何从以下位置获取字符串网址:

StorageReference file_path = mStorageReference.child("Posts_images")
    .child(currentUser + ".jpg");
file_path.putFile(pickedImageUri).addOnCompleteListener(new OnCompleteListener <UploadTask.TaskSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
        if (task.isSuccessful()) {
            //
            String msg = getString(R.string.imageUploadSuccessfully);
            Toast.makeText(AddPost_Activity.this, msg, Toast.LENGTH_SHORT).show();
            finish();
        }

2 个答案:

答案 0 :(得分:0)

在if语句中任务成功的地方,您可以像这样获得downloadUrl:

if (task.isSuccessful()) {

 String msg = getString(R.string.imageUploadSuccessfully);
 Toast.makeText(AddPost_Activity.this, msg, Toast.LENGTH_SHORT).show();

 file_path.getDownloadUrl().addOnSuccessListener(new 
    OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            String url = uri.toString();
            //do stuff
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
    // Handle any errors
        }
    });

}

文档以获取更多信息AWS ECS Error when running task: No Container Instances were found in your cluster

答案 1 :(得分:0)

您需要使用此

 .addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
            if (downloadUri != null) {
                String photoStringUrl = downloadUri.toString();
            }
        }
    }
});