Android Studio无法获取存储下载URL [重复]

问题描述 投票:0回答:3

我不知道为什么,但是我正在尝试的所有方法都不起作用。我确实需要从我的文件中获取我的Storgae DownloadUrl,但我总是得到类似的东西:com.google.android.gms.tasks ...

我正在搜索一个星期,并尝试了各种YouTube方法。

如果有人可以帮助我,我将非常感激。以下是我的代码的简要说明。

非常感谢

private void uploadFile() {
        if (mImageUri != null) {
            final StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
                    + "." + getFileExtension(mImageUri));

            Log.i("test456", mImageUri.toString());

            mUploadTask = fileReference.putFile(mImageUri)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {


                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable(){
                                @Override
                                public void run() {
                                    mProgressBar.setProgress(0);


                                }
                            }, 500);

                            Log.i("test789", fileReference.getDownloadUrl().toString());

                            Toast.makeText(UploadForm.this, "Upload successful", Toast.LENGTH_LONG).show();
                            //test string change Upload too
                            String storageUrl = "";
                            Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
                                    taskSnapshot.getMetadata().getReference().getDownloadUrl().toString());
                            String uploadId = mDatabaseRef.push().getKey();
                            mDatabaseRef.child(uploadId).setValue(upload);



                          //  FirebaseStorage.getInstance().getReference().child("uploads").getDownloadUrl();

                            final String downloadedUrl = taskSnapshot.getStorage().getDownloadUrl().toString();

                            Uri downloadURL = Uri.parse(mImageUri.toString());
                            String imageUrl = downloadURL.toString();

                            Log.i("test123", String.valueOf(mStorageRef.getDownloadUrl()));

                            Log.i("XXX URL PATH____",taskSnapshot.getMetadata().getReference().getDownloadUrl().toString());



                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(UploadForm.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                            mProgressBar.setProgress((int) progress);
                        }
                    });
        } else {
            Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
        }
    }

    private void openImagesActivity() {
        Intent intent = new Intent(this, ImagesActivity.class);
        startActivity(intent);
    }
}
android firebase firebase-storage
3个回答
1
投票

您能尝试这个吗:

fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
    // Download URL 
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
    // Errors
}
});

0
投票

您也可以使用这种方式来获取下载网址。

     mUploadTask = fileReference.putFile(mImageUri)
            .addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if (task.isSuccessful())
                    {
                        task.getResult().getStorage().getDownloadUrl()
                                .addOnSuccessListener(new OnSuccessListener<Uri>() {
                                    @Override
                                    public void onSuccess(Uri uri) {
                                        String downloadUrl= uri.toString();

                                    }
                                });
                    }
                    else
                    {

                    }
                }
            });

0
投票

快速响应,但我得到了解决方案:)

更改了我的代码后,现在可以使用!

  Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
                                while (!urlTask.isSuccessful());
                                Uri downloadUrl = urlTask.getResult();

                                //Log.d(TAG, "onSuccess: firebase download url: " + 
                                downloadUrl.toString()); 
                                Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),downloadUrl.toString());

                                String uploadId = mDatabaseRef.push().getKey();
                                mDatabaseRef.child(uploadId).setValue(upload);
    ```
© www.soinside.com 2019 - 2024. All rights reserved.