0

This is my UploadActivity. The app lets me select a file , but crashes as soon as I press upload button. the Toast message of OnFailedListener is displayed everytime. I am choosing the correctimage file with correct naming conventions , but still facing the same problem. I have reffered to the documentation as well as tutorials , and implemented almost the same methods. What am I doing wrong?

public class UploadActivity extends AppCompatActivity {
   Button selectfile,upload;
   TextView notify;
   Uri imguri;
   ProgressDialog progressDialog;
   FirebaseStorage storage;
   FirebaseDatabase database;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_upload);
       storage = FirebaseStorage.getInstance();
       database = FirebaseDatabase.getInstance();

       selectfile = findViewById(R.id.SelectFile);
       upload = findViewById(R.id.UploadFile);
       notify = findViewById(R.id.notify);

       selectfile.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if(ContextCompat.checkSelfPermission(UploadActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
                   selectPDF();
               }
               else{
                   ActivityCompat.requestPermissions(UploadActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},9);
               }

           }
       });

       upload.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if(imguri!=null){
                   uploadFile(imguri);
               }
               else{
                   Toast.makeText(UploadActivity.this, "Please Select a File", Toast.LENGTH_SHORT).show();
               }
           }
       });

   }

   private void uploadFile(Uri imguri) {
       final String filename = System.currentTimeMillis()+"";
       progressDialog = new ProgressDialog(this);
       progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
       progressDialog.setTitle("Uploading File... ");
       progressDialog.setProgress(0);
       progressDialog.show();
       StorageReference storageReference = storage.getReference();
       storageReference.child("Uploads").child(filename).putFile(imguri)
               .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

                   @Override
                   public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                       Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();

                       task.addOnSuccessListener(new OnSuccessListener<Uri>() {
                           @Override
                           public void onSuccess(Uri uri) {
                               String url = uri.toString();
                               DatabaseReference databaseReference = database.getReference();
                               databaseReference.child(filename).setValue(url).addOnCompleteListener(new OnCompleteListener<Void>() {
                                   @Override
                                   public void onComplete(@NonNull Task<Void> task) {
                                       if(task.isSuccessful()){
                                           Toast.makeText(UploadActivity.this, "Successfully Uploaded", Toast.LENGTH_SHORT).show();
                                       }
                                       else{
                                           Toast.makeText(UploadActivity.this, "Please Try Again", Toast.LENGTH_SHORT).show();
                                       }
                                   }
                               });
                           }
                       });
                   }
               }).addOnFailureListener(new OnFailureListener() {
           @Override
           public void onFailure(@NonNull Exception e) {
               Toast.makeText(UploadActivity.this, "File was not uploaded, please try again!", Toast.LENGTH_SHORT).show();
           }
       }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
           @Override
           public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
               int currentProgress = (int) (100*taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount());
               progressDialog.setProgress(currentProgress);
           }
       });

   }

   @Override
   public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
       if(requestCode==9 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
           selectPDF();
       }
       else{
           Toast.makeText(this, "Please Provide External Storage Permission", Toast.LENGTH_LONG).show();
       }
   }

   private void selectPDF() {

       Intent intent = new Intent();
       intent.setType("images/*");
       intent.setAction(Intent.ACTION_GET_CONTENT);
       startActivityForResult(intent,86);

   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

       super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == 86 && resultCode == RESULT_OK && data != null) {
           imguri = data.getData();
           notify.setText("File Selected : "+ data.getData().getLastPathSegment());
       } else {
           Toast.makeText(this, "Please Select a file", Toast.LENGTH_SHORT).show();
       }
   }
}
Sarr
  • 11
  • 5
  • What's the error that you get in `onFailure()`? – Alex Mamo Apr 10 '20 at 08:45
  • The app crashes and the toast message "File was not uploaded, please try again!" is displayed which means that the onFailureListener gets executed. – Sarr Apr 10 '20 at 09:01
  • 1
    Try to log the exception message. `Log.d("TAG", e.getException().getMessage());` and tell us what is printed out. – Alex Mamo Apr 10 '20 at 09:10
  • So I got two errors logged : 1.) StorageException has occurred. An unknown error occurred, please check the HTTP result code and inner exception for server response. 2.) android.view.WindowLeaked: Activity com.example.tradeapp.UploadActivity has leaked window DecorView@c108806[ ] that was originally added here. I believe , the first error caused the second , but not sure. – Sarr Apr 10 '20 at 10:23
  • Check **[this](https://stackoverflow.com/a/59093116/5246885)** out. – Alex Mamo Apr 10 '20 at 10:33
  • 1
    Thanks @AlexMamo . I changed storage version to latest version and now its working perfectly. – Sarr Apr 10 '20 at 17:34
  • Good to hear that ;) – Alex Mamo Apr 10 '20 at 17:48

1 Answers1

0

In build.gradle(module:app) , use the latest version for com.google.firebase:firebase-storage . Thanks Again @AlexMamo.

Sarr
  • 11
  • 5