Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
getDownloadUrl Firebase存储返回Null(Android)_Android_Firebase_Storage_Firebase Storage - Fatal编程技术网

getDownloadUrl Firebase存储返回Null(Android)

getDownloadUrl Firebase存储返回Null(Android),android,firebase,storage,firebase-storage,Android,Firebase,Storage,Firebase Storage,我正在开发一个小项目,我对firebase存储和getDownloadUrl有一些问题。 我已经在FirebaseStorage上上传了一些图像,但当我尝试获取下载Url时,它返回空值 代码如下: 进口: 函数getImage public void getImage(){ StorageReference myStorage = FirebaseStorage.getInstance().getReference(); StorageReference newStorage =

我正在开发一个小项目,我对firebase存储和getDownloadUrl有一些问题。 我已经在FirebaseStorage上上传了一些图像,但当我尝试获取下载Url时,它返回空值

代码如下: 进口:

函数getImage

public void getImage(){
    StorageReference myStorage = FirebaseStorage.getInstance().getReference();
    StorageReference newStorage = myStorage.child("picture").child("pic_one.jpg");
    newStorage.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            myuri = uri;
        }
    });
}
当应用程序运行getDownloadUrl行时,我的意思是我想检索https链接,以便使用glide在另一个活动中显示图片,但myuri变量的值为null。 变量myuri被定义为URI

提前感谢。

尝试这样做:

private String generatedFilePath;

 myStorage.child("picture").child("pic_one.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'pic_one.jpg'
            Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
            generatedFilePath = downloadUri.toString(); /// The string(file link) that you need
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });
然后,在成功地将图片上传到存储器后,抓取下载的URL并将其发布到数据库中

这是官方文件中的一个例子

    Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
    StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
    uploadTask = riversRef.putFile(file);
    
    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
            Uri downloadUrl = taskSnapshot.getDownloadUrl(); //After uploading your picture you can get the download url of it

mDatabase.child("images").setValue(downloadUrl); //and then you save it in your database
            
        }
    });

我在代码中没有看到检查myuri值的地方。你怎么知道它是空的?此外,您没有在任何地方检查错误。你确定getDownloadUrl返回的任务没有以错误结束吗?当我使用调试器运行应用程序时,我可以在myuri上看到null。它永远不会进入AddOnSuccessListener的内部,所以可能它正在生成一个错误。你只使用了一个成功的监听器。我刚刚添加了addOnFailureListener,甚至没有进入其中。
private String generatedFilePath;

 myStorage.child("picture").child("pic_one.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'pic_one.jpg'
            Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
            generatedFilePath = downloadUri.toString(); /// The string(file link) that you need
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });
private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();
    Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
    StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
    uploadTask = riversRef.putFile(file);
    
    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
            Uri downloadUrl = taskSnapshot.getDownloadUrl(); //After uploading your picture you can get the download url of it

mDatabase.child("images").setValue(downloadUrl); //and then you save it in your database
            
        }
    });
mDatabase.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    String downloadURL = dataSnapshot.getValue(String.class);
    //do whatever you want with the download url
  }