Bhupesh Bhupesh - 5 months ago 41
Android Question

Unable to find the file

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);




public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
paths = selectedImage.getPath();
Uri file = Uri.fromFile(new File(paths+".jpg"));
StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
UploadTask uploadTask = riversRef.putFile(file);

// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Log.i("Fail", String.valueOf(exception));
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.i("url", String.valueOf(downloadUrl));
}
});
}


I am trying to upload a file using firebase storage. Users can select a file and that file is to be uploaded. However, I am getting error on Uri file = Uri.fromFile(new File(paths+".jpg")); as /external/images/media/141792.jpg: open failed: ENOENT (No such file or directory)
java.io.FileNotFoundException: /external/images/media/141792.jpg: open failed: ENOENT (No such file or directory)

Answer

Try this out. It worked for me

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);


        if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
            Uri selectedImage = data.getData();
            final StorageReference photoRef = storageRef.child(selectedImage.getLastPathSegment());
            photoRef.putFile(selectedImage);
        }
    }