Android Studio无法获取存储下载URL

时间:2020-03-15 10:25:18

标签: android firebase firebase-storage

我不知道为什么,但是我尝试的所有方法都不起作用。 我确实需要从我的文件中获取我的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);
    }
}

3 个答案:

答案 0 :(得分:1)

您能尝试一下吗?

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

答案 1 :(得分: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
                    {

                    }
                }
            });

答案 2 :(得分:0)

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

在我的代码中进行了此更改,现在可以正常使用!!

  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);
    ```