Skip to content Skip to sidebar Skip to footer

Cannot Upload Image To App And Firebase Android Studio

please help me, I success to pick an image with a gallery and camera. but image not showing in image view or firebase storage. this is my manifest

Solution 1:

The way you're getting the download URL looks fishy. Having any sort of while (!uriTask.isSuccessful()); in your Android code is unlikely to ever give the desired result.

This is how I'd normalize it:

Stringfile= storagePath+ ""+ profilorcover +"_"+ user.getUid();

StorageReferencestorageReference2= storageReference.child(file);
storageReference2.putFile(uri).continueWithTask(newContinuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Overridepublic Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task)throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URLreturn ref.getDownloadUrl();
    }
}).addOnCompleteListener(newOnCompleteListener<Uri>() {
    @OverridepublicvoidonComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            UridownloadUri= task.getResult();

            HashMap<String, Object> results = newHashMap<>();

            results.put(profilorcover, downloaduri.toString());

            databaseReference.child(user.getUid()).updateChildren(results)
                    .addOnSuccessListener(newOnSuccessListener<Void>() {
                        @OverridepublicvoidonSuccess(Void aVoid) {

                            progressDialog.dismiss();
                            Toast.makeText(getActivity(), "Mengubah Gambar...", Toast.LENGTH_SHORT).show();
                        }
                }
            });
        } else {
            progressDialog.dismiss();
            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
});

This is a pretty direct merge of your code with the flow from the Firebase documentation on getting a download URL after uploading an image.

Solution 2:

That problem in the code requestcode == result_ok. That must change like resultcode==result_ok. That's why my problem solved.

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

    if (requestCode == RESULT_OK){

requestCode replace wirh resultCode.

        if(requestCode == IMAGE_PICK_GALLERY_CODE){
            image_uri = data.getData();
            uploadprofile(image_uri);
        }
        if (requestCode == IMAGE_PICK_CAMERA_CODE){
            uploadprofile(image_uri);
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Post a Comment for "Cannot Upload Image To App And Firebase Android Studio"