taskSnapshot.getDownloadUrl() is deprecated

前端 未结 10 1383
时光说笑
时光说笑 2020-11-27 19:39

Until now, the way to get the url from file on Storage in Firebase, I used to do this taskSnapshot.getDownloadUrl, but nowadays is deprecated, which method I

相关标签:
10条回答
  • 2020-11-27 20:26

    Add the code below:

    Task<Uri> downUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl();
    Log.i("url:",downUrl.getResult().toString());
    
    0 讨论(0)
  • 2020-11-27 20:28

    Just use a Task instead of ref.putFile(uriImage) .addOnSuccessListener(new OnSuccessListener() Nowadays, firebase references suggest using Uploadtask objects

    I've done it like this:

    UploadTask uploadTask;
            uploadTask = storageReferenceProfilePic.putFile(uriProfileImage );
    
            Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }
    
                    // Continue with the task to get the download URL
    
                    return storageReferenceProfilePic.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()) {
                        progressBarImageUploading.setVisibility(View.GONE);
                        Uri downloadUri = task.getResult();
                        profileImageUrl = downloadUri.toString();
                        ins.setText(profileImageUrl);
                    } else {
                        // Handle failures
                        // ...
                    }
                }
            });
    

    Notice these lines in the above code:

    Uri downloadUri = task.getResult();
    profileImageUrl = downloadUri.toString();
    

    Now profileImageUrl contains something like "http://adressofimage" which is the url to acess the image

    Now you're free to use the String profileImageUrl however you wish. For e.g., load the url into an ImageView using Glide or Fresco libraries.

    0 讨论(0)
  • 2020-11-27 20:29

    As Doug says, you will need to run it inside a Task

    Here is a hint of how you need to implement it

    final StorageReference ref = storageRef.child("your_REF");
    uploadTask = ref.putFile(file);
    
        Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }
    
                // Continue with the task to get the download URL
                return ref.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    String downloadURL = downloadUri.toString();
                } else {
                    // Handle failures
                    // ...
                }
            }
        });
    

    For more information about how to implement it, you can check this github question that was opened 7 days after I answered this https://github.com/udacity/and-nd-firebase/issues/41

    0 讨论(0)
  • 2020-11-27 20:31

    This code work for me.

    You can try.

    package br.com.amptec.firebaseapp;
    
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.provider.ContactsContract;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.OnFailureListener;
    import com.google.android.gms.tasks.OnSuccessListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.auth.AuthResult;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.Query;
    import com.google.firebase.database.ValueEventListener;
    import com.google.firebase.storage.FirebaseStorage;
    import com.google.firebase.storage.StorageReference;
    import com.google.firebase.storage.UploadTask;
    
    import java.io.ByteArrayOutputStream;
    import java.util.UUID;
    
    public class MainActivity extends AppCompatActivity {
    
        private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        private FirebaseAuth auth = FirebaseAuth.getInstance();
    
        private Button btnUpload;
        private ImageView imgPhoto;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnUpload = findViewById(R.id.btnUpload);
            imgPhoto = findViewById(R.id.imgPhoto);
    
            btnUpload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    imgPhoto.setDrawingCacheEnabled(true);
                    imgPhoto.buildDrawingCache();
                    Bitmap bitmap = imgPhoto.getDrawingCache();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                    byte[] imageBytes = baos.toByteArray();
                    String fileName = UUID.randomUUID().toString();
    
                    StorageReference storageReference = FirebaseStorage.getInstance().getReference();
                    StorageReference images = storageReference.child("images");
                    StorageReference imageRef = images.child(fileName + ".jpeg");
    
                    UploadTask uploadTask = imageRef.putBytes(imageBytes);
    
                    uploadTask.addOnFailureListener(MainActivity.this, new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(MainActivity.this, "Upload Error: " +
                                    e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }).addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            //Uri url = taskSnapshot.getDownloadUrl();
                            Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
                            while(!uri.isComplete());
                            Uri url = uri.getResult();
    
                            Toast.makeText(MainActivity.this, "Upload Success, download URL " +
                                    url.toString(), Toast.LENGTH_LONG).show();
                            Log.i("FBApp1 URL ", url.toString());
                        }
                    });
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题