-2

Hello I got an error while uploading image to Firebase. I run this same code in activity when I set up profile and there it runs perfect but when I got this in activity where you can change profile picture there come an error.

Error message

Exception java.lang.RuntimeException: Failure delivering result 

ResultInfo{who=null, request=2, result=-1, data=Intent { dat=file:///storage/emulated/0/DCIM/Facebook/FB_IMG_1476211508746.jpg typ=image/jpeg }} to activity {com.samo.facedatefb/com.samo.facedatefb.Settings}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
android.app.ActivityThread.deliverResults (ActivityThread.java:3741)
android.app.ActivityThread.handleSendResult (ActivityThread.java:3784)
android.app.ActivityThread.access$1300 (ActivityThread.java:169)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1472)
android.os.Handler.dispatchMessage (Handler.java:11
android.os.Looper.loop (Looper.java:194)
android.app.ActivityThread.main (ActivityThread.java:5546)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:967)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)
arrow_drop_down
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
com.samo.facedatefb.Settings.onActivityResult (Settings.java:232)
android.app.Activity.dispatchActivityResult (Activity.java:6177)
android.app.ActivityThread.deliverResults (ActivityThread.java:3737)
android.app.ActivityThread.handleSendResult (ActivityThread.java:3784)
android.app.ActivityThread.access$1300 (ActivityThread.java:169)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1472)
android.os.Handler.dispatchMessage (Handler.java:111)
android.os.Looper.loop (Looper.java:194)
android.app.ActivityThread.main (ActivityThread.java:5546)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:967)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)

And here is code

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

    if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
        mProgressDialog.setMessage("Uploading...");
        mProgressDialog.show();

        StorageReference imageRef = mStorage.child(myEmail);
        StorageReference imageImagesRef = mStorage.child("photos/" + myEmail);

        imageRef.getName().equals(imageImagesRef.getName());    // true
        imageRef.getPath().equals(imageImagesRef.getPath());    // false


        Uri uri = data.getData();
        imageView.setImageURI(uri);

        imageView.setDrawingCacheEnabled(true);
        imageView.buildDrawingCache();
        Bitmap bitmap = imageView.getDrawingCache();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] data2 = baos.toByteArray();

        UploadTask uploadTask = imageImagesRef.putBytes(data2);
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                Toast.makeText(getApplicationContext(), "Sorry, your image wasn't set, please try again",
                        Toast.LENGTH_LONG).show();
            }
        }).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.
                mProgressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Upload done",
                        Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
                startActivity(intent);
            }
        });
    }
} 

and

} else if (arrayList.get(position).equals("Change profile photo")) {
       Intent intent = new Intent(Intent.ACTION_PICK);
       intent.setType("image/*");
       startActivityForResult(intent, GALLERY_INTENT);
}
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
Sam
  • 179
  • 1
  • 10
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Chisko Nov 30 '16 at 18:14

1 Answers1

1
// This is null  
 Uri uri = data.getData();

So before compressing bitmap check null pointer and try to find tutorial for pulling Images from Gallery. And also check for Runtime permission if your device's Android version is marshmallow or later.

Rahul
  • 9,559
  • 4
  • 32
  • 53
  • Thanks, but how is possible that in another activity(in same app on same device at the same time) it works? :) – Sam Nov 30 '16 at 18:19
  • @Sam Try to create bitmap from file path instead of drawing cache. http://stackoverflow.com/questions/16804404/create-a-bitmap-drawable-from-file-path – Rahul Nov 30 '16 at 18:27