0

Im new on android, and in this app, you need to fill some edittext, like name, email, etc, then add a picture using a button that opens gallery or another button that takes a picture and and after this you have to push a button to send this info to a db. It works if i add the picture from the gallery, and send this info, but when i do the same taking a picture the app crashes. Im not sure what could be the problem and i'd really appreciate someone help me out. Or tell me what can be causing this problem.

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

    if(requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK ){

try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), (filePath));
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, filePath));
} 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:

08-20 23:23:53.320 11022-11022/com.example.lupitagarcia.yosoyvallarta E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.lupitagarcia.yosoyvallarta, PID: 11022
                                                                                    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:579)
                                                                                        at com.example.lupitagarcia.yosoyvallarta.TerminosYC$1.onClick(TerminosYC.java:355)
                                                                                        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) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Daniel Prado
  • 79
  • 11
  • `cursor.moveToFirst();` this code is throwing error means cursor is null. – Arjun Gurung Aug 21 '17 at 04:47
  • But i dont know why is this. or how to solve it, since it doesnt happen the same when attaching a picture from gallery. @ArjunGurung – Daniel Prado Aug 21 '17 at 04:50
  • Use different Cursor variable in this line: 'cursor = getActivity().getContentResolver().query( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);' – Danger Aug 21 '17 at 06:23
  • You mean like saying if requestcode== CAM_REQUEST then 'cursor = getActivity().getContentResolver().query( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_UR‌​I, null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null); or what do you mena buddy? @Danger – Daniel Prado Aug 21 '17 at 17:39
  • Closing cursor could be the problem. Try using different Cursor variables. Define two cursor variables. public String getPath(Uri uri) { Cursor cursor2 = getActivity().getContentResolver().query( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null); cursor2.moveToFirst(); String path = cursor2.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); cursor2.close(); return path; } – Danger Aug 22 '17 at 05:55
  • Thanks, i will do it, and see what happens! – Daniel Prado Aug 22 '17 at 12:40

1 Answers1

0

Seems no efforts! anyway try this

if(mCursor!=null && mCursor.getCount()>0 ){
    mCursor.moveToFirst();
}

The reason is your #uri ,as you are getting null value there. First make sure that you are getting proper image uri or not!

Radhey
  • 2,139
  • 2
  • 24
  • 42