storageReference.getDownloadUrl()为存储在firebase中的音频文件返回null

时间:2017-10-27 06:01:48

标签: android firebase firebase-storage

我正在尝试获取存储在Firebase存储中的mp3文件的下载URL。 firebase上存储的路径是“Categories / numbers / one.mp3”。 下面是我用来获取下载Url的代码但看起来它不起作用。我将downloadUrl视为null。

有人可以帮忙吗?

代码:

    mediaPlayer = new MediaPlayer();

            storageReference = FirebaseStorage.getInstance().getReference().child("Categories").child(mCategory).child("one.mp3");
            storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    try {
                        mediaPlayer.setDataSource(uri.toString());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

2 个答案:

答案 0 :(得分:0)

你应该这样做

storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                try {
                      @SuppressWarnings("VisibleForTests") Uri downloadUrl = 
                         taskSnapshot.getDownloadUrl();
                        uploadImageUrl = downloadUrl.toString();
                    Toast.makeText(getApplicationContext(), "url to your file..."+uploadImageUrl, Toast.LENGTH_SHORT).show();
                    //rest your code
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

答案 1 :(得分:0)

希望这可能有用。!

storageRef.child("Categories").child(mCategory).child("one.mp3").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {

try {
                    mediaPlayer.setDataSource(uri.toString());
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
});
// Alternative way
storageRef.child("Categories").child(mCategory).child("one.mp3").getDownloadUrl().getResult();


 StorageReference storageRef = FirebaseStorage.getInstance().getReference();