0

This is my code for uploading image on server,This work fine with other projects but i got error when i am using this code in my project //method to get the file path from uri

 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;
    }

I am using this library for uploading an image : compile 'net.gotev:uploadservice:2.1' what should i do???

6
  • what error are you getting? show error Log Commented Jul 12, 2017 at 10:55
  • from this method you get path right Commented Jul 12, 2017 at 10:57
  • Share your stack traces. It will be useful for us to identify the issue and help you out. Commented Jul 12, 2017 at 11:01
  • Which type of request you are using Multipart Post or Multipart Put for image uploading.. Commented Jul 12, 2017 at 11:04
  • android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at this line : String paths = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); I have to upload image to server. Commented Jul 12, 2017 at 11:17

1 Answer 1

1

You just got problem because cursor is null at that line. You have to check null when work with cursor:

String path = ""; 
if(cursor != null && cursor.moveToFirst()){
    path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
    cursor.close();
}
Sign up to request clarification or add additional context in comments.

5 Comments

actually it's not null, it's empty. as per OP comment the error is android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 ...
If the path is empty, you need to check before convert to byte[] and pass into multipart. If the path is empty, you get no image. Can you post ur code when you convert path to byte and when you use multipart to post image ?
This code works fine on all projects but when i use it on my project i am getting null path.I don't know why?
I worked on many kind of android device. MediaStore.Images.Media.DATA not return real path for image on all device. Tomorrow i will give you an class that i wrote to get image path. This class work on almost device.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.