1

New on android, i have one button to open gallery select a picture and display it into an imageview, which works properly, and another button to open the camera using Intent, i can open the camera take a picture but after clicking save image the app crashes. The image taken is saved in the correct folder. and when debbugin the app the error thrown its on this line:

filePath = data.getData();

and bitmap shows it as null. I have added the permissions and i have read many post about this but still can't figure it out what is the problem. Any help is appreciated. Here's the code.

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

    if(requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK ){
        Toast.makeText(TerminosYC.this.getActivity(), "si hay foto", Toast.LENGTH_LONG).show();

filePath = data.getData();

try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

            areaLeyendaImagen.setVisibility(View.VISIBLE);
            areaFotoCargada.setVisibility(View.VISIBLE);
            imagencargada.setVisibility(View.VISIBLE);
            textoimagencargada.setVisibility(View.VISIBLE);
            area_cerrarIagen.setVisibility(View.VISIBLE);
            cerrarImagen.setVisibility(View.VISIBLE);

            areaLeyendaImagen.setFocusableInTouchMode(true);
            areaLeyendaImagen.requestFocus();
            areaLeyendaImagen.setFocusable(true);
            buttonChoose.setError(null);

        } catch (IOException e) {
            e.printStackTrace();
        }

}else if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK ) {
        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

            areaLeyendaImagen.setVisibility(View.VISIBLE);
            areaFotoCargada.setVisibility(View.VISIBLE);
            imagencargada.setVisibility(View.VISIBLE);
            textoimagencargada.setVisibility(View.VISIBLE);
            area_cerrarIagen.setVisibility(View.VISIBLE);
            cerrarImagen.setVisibility(View.VISIBLE);
            buttonChoose.setError(null);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 public String getPath(Uri uri) {
    Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getActivity().getContentResolver().query(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}

LOGCAT:

a E/MotionRecognitionManager: mSContextService = null
08-13 23:50:38.243 27745-27745/com.example.lupitagarcia.yosoyvallarta 
E/MotionRecognitionManager: motionService = 
com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@39e18a4
08-13 23:50:51.483 27745-27745/com.example.lupitagarcia.yosoyvallarta 
E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.lupitagarcia.yosoyvallarta, PID: 27745
                                                                                    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66849, result=-1, data=null} to activity {com.example.lupitagarcia.yosoyvallarta/com.example.lupitagarcia.yosoyvallarta.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
                                                                                        at android.app.ActivityThread.deliverResults(ActivityThread.java:5004)
                                                                                        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047)
                                                                                        at android.app.ActivityThread.access$1600(ActivityThread.java:229)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7406)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
                                                                                        at com.example.lupitagarcia.yosoyvallarta.TerminosYC.onActivityResult(TerminosYC.java:463)
                                                                                        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
                                                                                        at android.app.Activity.dispatchActivityResult(Activity.java:7165)
                                                                                        at android.app.ActivityThread.deliverResults(ActivityThread.java:5000)
                                                                                        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047) 
                                                                                        at android.app.ActivityThread.access$1600(ActivityThread.java:229) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7406) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

UPDATE:

Once the picture is attached on the imageview, and i click on a button to send the picture to store it in a db, i get this error.

FATAL EXCEPTION: main
                                                                                    Process: com.example.lupitagarcia.yosoyvallarta, PID: 15254
                                                                                    java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
                                                                                        at com.example.lupitagarcia.yosoyvallarta.TerminosYC.getPath(TerminosYC.java:549)
                                                                                        at com.example.lupitagarcia.yosoyvallarta.TerminosYC$1.onClick(TerminosYC.java:352)
                                                                                        at android.view.View.performClick(View.java:5721)
                                                                                        at android.widget.TextView.performClick(TextView.java:10936)
                                                                                        at android.view.View$PerformClick.run(View.java:22620)
                                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7406)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Daniel Prado
  • 79
  • 11
  • Are you testing it in a Samsung device by chance? – Enzokie Aug 14 '17 at 05:38
  • Yes it's a samsung device. @Enzokie – Daniel Prado Aug 14 '17 at 05:42
  • It's a bug in Samsung ;) Therefore assume that getData will be null at some point. – Enzokie Aug 14 '17 at 05:43
  • I've tested it before using Intent but saving the picture on the phone memory and it work property, but had to change that because it didn't work on nougat devices. @Enzokie – Daniel Prado Aug 14 '17 at 05:45
  • If you remember before you open the Camera Intent you are actually providing a `Uri` in that case hold that `Uri` instance until you save your image. Do not rely on `getData` because it has its own issue. Do not also forget to retain its instance because Samsung is notorious in killing background Activities base on my experience. – Enzokie Aug 14 '17 at 05:47
  • All right, thanks for the information, i will check that out. – Daniel Prado Aug 14 '17 at 06:17
  • this not perfect solution but check this once may you get your answer: https://stackoverflow.com/a/44964221/6021469 – Shweta Chauhan Aug 14 '17 at 07:07

4 Answers4

1

try this

values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
     MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_CLICK_RESULT);

and on onActivityResult

 if (requestCode == CAMERA_CLICK_RESULT && resultCode == RESULT_OK) {
        Bitmap photo = null;
        try {
            photo = MediaStore.Images.Media.getBitmap(
                    getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
   }
Ashutosh Sagar
  • 981
  • 8
  • 20
0

I don't know what your codes do. But, the log says that: the application has been crashed because of null pointer exception.

Check the main method of ZygoteInit.java class . At 1120 line, you try to get something from null instance.

You should fix this.

ziLk
  • 3,120
  • 21
  • 45
0

I think problem is here ,, intead of this

bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);

change like this

Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Omar Dhanish
  • 885
  • 7
  • 18
  • Sorry, what would it be ImagePicker, i got the 'cannot resolve symbol' on this – Daniel Prado Aug 14 '17 at 05:32
  • did you add this line ??? try adding this line in Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); and try with your own code bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath); – Omar Dhanish Aug 14 '17 at 05:54
  • I already add that line, used bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentRe‌​solver(), filePath); and the app doesn't crash after saving saving the picture, but it crashes in another part of my code. let me show you to see if you can help me please – Daniel Prado Aug 14 '17 at 06:07
  • this is the line cursor.moveToFirst(); – Daniel Prado Aug 14 '17 at 06:08
0

You can get Bitmap When REQUEST_CAMERA

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

You can get Bitmap When PICK_IMAGE_REQUEST from gallery

   Uri uri = data.getData();
  try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(mcontext.getContentResolver(), uri);
                // Log.d(TAG, String.valueOf(bitmap));
     } catch (Exception e) {
                    e.printStackTrace();
     }
Yogesh Mane
  • 403
  • 6
  • 18