1

I am already successful in posting images to Firebase Storage but I am wondering if it is possible to have a link/url in Firebase Database from which I can click to redirect me to Firebase Storage to see that image.

I want this function so that along with user inputs such as Title, Date, Remarks, the end-user would be able to see the "image" child with the link along with other inputs

I have tried searching StackOverflow and Youtube for answers but most of them are old and seem outdated. There is a command getDownloadUrl, but I believe it has been deprecated.

This is the code from my class that uploads my image to Firebase Storage. where to add storagerefernce.downloadUrl().

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    storage = FirebaseStorage.getInstance();
    storageReference = storage.getReference();
    btnChoose = (ImageButton) findViewById(R.id.btnChoose);

    imageView = (ImageView) findViewById(R.id.imgView);
    btnChoose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            chooseImage();
        }
    });


}
public void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE_REQUEST);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null )
    {
        filePath = data.getData();
        imgname = filePath.getLastPathSegment();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
            uploadImage();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

private void uploadImage()
{

    if(filePath != null)
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading...");
        progressDialog.show();

        StorageReference ref = storageReference.child("images/"+ imgname);
        ref.putFile(filePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();
                        Toast.makeText(MainActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(MainActivity.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                .getTotalByteCount());
                        progressDialog.setMessage("Uploaded "+(int)progress+"%");
                    }
                });
        }
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sanath Ks
  • 13
  • 3
  • can you please help me find what is wrong with my [code](https://github.com/sanathks1998/sanathks1/blob/master/geturloffirebasepic) – Sanath Ks Mar 04 '19 at 16:40

1 Answers1

0

You can try this way.

  val ref = mStorageReference?.child("images/mMobileNumber.jpg")
        val  uploadTask = ref?.putFile(Uri.fromFile(File(mImagePath)))

        uploadTask?.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>>
        { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    throw it
                }
            }
            return@Continuation ref.downloadUrl
        })?.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val downloadUri = task.result
                mTempDatabaseReference?.child("image")?.setValue(downloadUri.toString())
            } else {
                // Handle failures
                // ...
            }
        }
Rajat Mittal
  • 413
  • 5
  • 19
  • can you please help me find what is wrong with my [link](https://github.com/sanathks1998/sanathks1/blob/master/geturloffirebasepic)code – Sanath Ks Mar 04 '19 at 15:29
  • @SanathKs First of all, You are getting url in `onprogresslistener`. Always fetch url from `addOnCompleteListener`. Second, Prefer using my way, it is easy and works always. Third, Dont forget to upvote my answer if it helped you in anyway. – Rajat Mittal Mar 04 '19 at 18:07