0

I am trying to send image and image name to my php. When I run script through postman image inserted successfully but when I try to send image name and image through android app it give me error in this section of code:

 public String getPath(Uri uri) {
        Cursor cursor = 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 = 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;
    }

this is the error log

FATAL EXCEPTION: main
    Process: com.mannan.restaurantapp, PID: 3246
    android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at android.database.CursorWrapper.getString(CursorWrapper.java:137)
        at com.mannan.restaurantapp.Activites.AddItems.getPath(AddItems.java:139)
        at com.mannan.restaurantapp.Activites.AddItems.uploadMultipart(AddItems.java:82)
        at com.mannan.restaurantapp.Activites.AddItems.onClick(AddItems.java:186)
        at android.view.View.performClick(View.java:5637)
TIGER
  • 2,864
  • 5
  • 35
  • 45
Naveed Abbas
  • 220
  • 3
  • 10

1 Answers1

0

you need to send source of image with its path something like this

File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
    Uri imgUri = Uri.fromFile(file);
    this.imgPath = file.getAbsolutePath();
    return imgUri;

onece you get the path send it to server.

umesh vashisth
  • 339
  • 3
  • 16