如何在使用FirebaseStorage时声明最终的Uri?

时间:2017-03-14 09:32:43

标签: android firebase firebase-storage

我从FirebaseStorage中获取引用,我想保存图片网址的值,这是我的代码:

//FirebaseStorage
                String path="firememes/"+ UUID.randomUUID()+".png";
                StorageReference firememeRef=storage.getReference(path);

                StorageMetadata metadata=new StorageMetadata.Builder()
                        .setCustomMetadata("text","測試").build();

                 Uri url;
                UploadTask uploadTask=firememeRef.putBytes(bytes,metadata);
                StorageTask<UploadTask.TaskSnapshot> taskSnapshotStorageTask = uploadTask.addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                         //it shows inner class , need to declared final
                         url = taskSnapshot.getDownloadUrl();
                        Log.d("url>",url.toString());

                    }
                });

但是当我改为最后的决赛Uri url时;它显示最终变量'url'

的值不能

我想获取url.toString值并保存它,如何为Uri设置全局变量,任何帮助都将不胜感激,提前感谢。

1 个答案:

答案 0 :(得分:2)

在回调中定义变量url,而不是在回调之外。

String path="firememes/"+ UUID.randomUUID()+".png";
StorageReference firememeRef=storage.getReference(path);

StorageMetadata metadata=new StorageMetadata.Builder()
        .setCustomMetadata("text","測試").build();

UploadTask uploadTask=firememeRef.putBytes(bytes,metadata);
StorageTask<UploadTask.TaskSnapshot> taskSnapshotStorageTask = uploadTask.addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        //it shows inner class , need to declared final
        Uri url = taskSnapshot.getDownloadUrl();
        Log.d("url>",url.toString());
    }
});