Firebase how to get Image Url from firebase storage?

后端 未结 12 579
悲&欢浪女
悲&欢浪女 2020-12-23 15:15

Right now i am fetching image from Storage of Firebase by using below code :

mStoreRef.child("photos/" + model.getBase64Image())
          .getDownl         


        
相关标签:
12条回答
  • 2020-12-23 15:21

    Thats works fine on latest firebase builds

    UploadTask uploadTask = ref.putBytes(data);
    uploadTask.addOnSuccessListener(new OnSuccessListener<TaskSnapshot>() {
    
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                taskSnapshot.getStorage().getDownloadUrl().addOnCompleteListener(
                        new OnCompleteListener<Uri>() {
    
                               @Override
                                public void onComplete(@NonNull Task<Uri> task) {
                                String fileLink = task.getResult().toString();
                                //next work with URL
    
                                }
                            });
    
    0 讨论(0)
  • 2020-12-23 15:21

    I tried this and it got me the link to review it on the imageview :

    storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'users/me/profile.png'
            uri.toString();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });
    
    0 讨论(0)
  • 2020-12-23 15:22

    This should be how it should be done in kotlin you need to add an onCompleteListener

    putFile(file).addOnSuccessListener {
    
                it.storage.downloadUrl.addOnCompleteListener {
                //then you can call it.result.toString()
                }
    }
    
    0 讨论(0)
  • 2020-12-23 15:23

    This works for me with dependency - implementation 'com.google.firebase:firebase-storage:19.1.0'

    ref.putFile(iconUriLocalFilePath)
            .addOnSuccessListener(  
                new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(
                        UploadTask.TaskSnapshot taskSnapshot)
                {
                    Task<Uri> uri = ref.getDownloadUrl();
                    String iconPathFirebase = uri.getResult().toString();
                }
        });
    
    0 讨论(0)
  • 2020-12-23 15:30

    Follow this link -https://firebase.google.com/docs/storage/android/download-files#download_data_via_url

        storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                // Got the download URL for 'users/me/profile.png'
                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
            }
        });
    
    0 讨论(0)
  • 2020-12-23 15:32

    As per the latest firebase changes, here is the updated code:

    File file = new File(String.valueOf(imageUri));         
    FirebaseStorage storage = FirebaseStorage.getInstance();        
    StorageReference storageRef = storage.getReference().child("images");
    
    storageRef.child(file.getName()).putFile(imageUri)
        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                pd.dismiss();
                Toast.makeText(MyProfile.this, "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
                Task<Uri> downloadUri = taskSnapshot.getStorage().getDownloadUrl();
    
               if(downloadUri.isSuccessful()){
                String generatedFilePath = downloadUri.getResult().toString();
                System.out.println("## Stored path is "+generatedFilePath);
            }}
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                pd.dismiss();                     
            }
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题