0

I am trying to run a code but I am getting an error since that method has been removed but I am not able to understand what should I write instead of

Upload upload = new Upload(editTextName.getText().toString().trim(), taskSnapshot.getDownloadUrl().toString());"

I am able to see the solution is given in the following:

Error: cannot find symbol method getDownloadUrl() of type com.google.firebase.storage.UploadTask.TaskSnapshot

but it is difficult to find the solution from the page given to the link.

Let me know what should I write?

The whole code:

    package com.example.firebasestorageexample;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.ProgressDialog;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.webkit.MimeTypeMap;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.google.android.gms.tasks.OnFailureListener;
    import com.google.android.gms.tasks.OnSuccessListener;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.storage.FirebaseStorage;
    import com.google.firebase.storage.OnProgressListener;
    import com.google.firebase.storage.StorageReference;
    import com.google.firebase.storage.UploadTask;
    
    import java.io.IOException;
    import java.sql.DatabaseMetaData;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        //constant to track image chooser intent
        private static final int PICK_IMAGE_REQUEST = 234;
        //view objects
        private Button buttonChoose;
        private Button buttonUpload;
        private EditText editTextName;
        private TextView textViewShow;
        private ImageView imageView;
    
        //uri to store file
        private Uri filePath;
    
        //firebase objects
        private StorageReference storageReference;
        private DatabaseReference mDatabase;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            buttonChoose = (Button) findViewById(R.id.buttonChoose);
            buttonUpload = (Button) findViewById(R.id.buttonUpload);
            imageView = (ImageView) findViewById(R.id.imageView);
            editTextName = (EditText) findViewById(R.id.editText);
            textViewShow = (TextView) findViewById(R.id.textViewShow);
            storageReference = FirebaseStorage.getInstance().getReference();
            mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
    
            buttonChoose.setOnClickListener(this);
            buttonUpload.setOnClickListener(this);
            textViewShow.setOnClickListener(this);
        }
        private void showFileChooser() {
            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();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                    imageView.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        @Override
        public void onClick(View view) {
            if (view == buttonChoose) {
                showFileChooser();
            } else if (view == buttonUpload) {
                uploadFile();
            } else if (view == textViewShow) {
    
            }
        }
        public String getFileExtension(Uri uri) {
            ContentResolver cR = getContentResolver();
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            return mime.getExtensionFromMimeType(cR.getType(uri));
        }
        private void uploadFile() {
            //checking if file is available
            if (filePath != null) {
                //displaying progress dialog while image is uploading
                final ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setTitle("Uploading");
                progressDialog.show();
    
                //getting the storage reference
                StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "." + getFileExtension(filePath));
    
                //adding the file to reference
                sRef.putFile(filePath)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                //dismissing the progress dialog
                                progressDialog.dismiss();
                                //displaying success toast
                                Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
    
                                //creating the upload object to store uploaded image details
                                Upload upload = new Upload(editTextName.getText().toString().trim(), taskSnapshot.getDownloadUrl().toString());
    
                                //adding an upload to firebase database
                                String uploadId = mDatabase.push().getKey();
                                mDatabase.child(uploadId).setValue(upload);
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
                                progressDialog.dismiss();
                                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        })
                        .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                                //displaying the upload progress
                                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                                progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
                            }
                        });
            } else {
                //display an error if no file is selected
            }
        }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • we don't even know what you are trying to do. If that method has been removed, look at previous versions of the documentation, it will probably mention that it has been deprecated, and what other functionality replaced it. – Stultuske Mar 24 '21 at 07:33
  • That's not the way to get the download URL. Please check the duplicate to see how you can achieve that. – Alex Mamo Mar 24 '21 at 08:18
  • Determining the download URL requires a call to the server. Because of this, the call to `getDownloadUrl()` returns a `Task` that completes when the download URL comes back from the server. You'll need to call `addSuccessListener()` on it to wait for it to complete. See the documentation [here](https://firebase.google.com/docs/storage/android/download-files#download_data_via_url) and this [answer](https://stackoverflow.com/a/51064689) – Frank van Puffelen Mar 24 '21 at 14:38

1 Answers1

0
 taskSnapshot.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                           File file = new File(uri.getPath());
                           Upload upload = new Upload(editTextName.getText().toString().trim(), file.getPath());
                        }
                    });
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ulai Nava
  • 167
  • 1
  • 1
  • 12
  • 1
    Instead of writing "taskSnapshot.getDownloadUrl().toString()" we can write "taskSnapshot.getStorage().getDownloadUrl().toString()" that is working for me. – sanjay gupta Mar 26 '21 at 05:47