2

i am getting image from gallery from below code

 Uri filePath = data.getData();
 String[] filePathColumn = {MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.MIME_TYPE};
            Cursor cursor =
                    mCon.getContentResolver().query(filePath, filePathColumn, null, null, null);

but when i select image from file explore in some devices cursor is always null but if i select same image from gallery app it works... why it so ?

andro
  • 1,007
  • 1
  • 14
  • 32

3 Answers3

5

try this code

private String uriToFilename(Uri uri) {
        String path = null;

        if (Build.VERSION.SDK_INT < 11) {
            path = RealPathUtil.getRealPathFromURI_BelowAPI11(this, uri);
        } else if (Build.VERSION.SDK_INT < 19) {
            path = RealPathUtil.getRealPathFromURI_API11to18(this, uri);
        } else {
            path = RealPathUtil.getRealPathFromURI_API19(this, uri);
        }

        return path;
    }

create RealOathUtil.java

public class RealPathUtil {
    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API19(Context context, Uri uri) {
        Log.e("uri", uri.getPath());
        String filePath = "";
        if (DocumentsContract.isDocumentUri(context, uri)) {
            String wholeID = DocumentsContract.getDocumentId(uri);
            Log.e("wholeID", wholeID);
// Split at colon, use second item in the array
            String[] splits = wholeID.split(":");
            if (splits.length == 2) {
                String id = splits[1];

                String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
                String sel = MediaStore.Images.Media._ID + "=?";
                Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        column, sel, new String[]{id}, null);
                int columnIndex = cursor.getColumnIndex(column[0]);
                if (cursor.moveToFirst()) {
                    filePath = cursor.getString(columnIndex);
                }
                cursor.close();
            }
        } else {
            filePath = uri.getPath();
        }
        return filePath;
    }

    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        String result = null;
        CursorLoader cursorLoader = new CursorLoader(
                context,
                contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
        if (cursor != null) {
            int column_index =
                    cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
        return result;
    }

    public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index
                = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}

Now in onactivtyResult() use this

Uri selectedImageUri = data.getData();
 String selectedImagePath = uriToFilename(selectedImageUri);
Mustanser Iqbal
  • 5,017
  • 4
  • 18
  • 36
0

You should try this.

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
PRIYA PARASHAR
  • 777
  • 4
  • 15
  • i am using Intent intent = new Intent(); intent.setType("*/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); – andro Apr 12 '16 at 08:00
0

Try this code.

To open gallery:

Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_PICK);
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        Uri selectedImageUri = data.getData();
        mImagePath = getPath(selectedImageUri);
        mBitmap = BitmapFactory.decodeFile(mImagePath);
        mProfilePic.setImageBitmap(mBitmap);
    }
}

To get actual path of image:

public String getPath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Hope this may help you.

Mrunal
  • 348
  • 4
  • 15
  • nope in this code it give cursor null if select image from file exploere – andro Apr 12 '16 at 08:24
  • this is same cod ei have wriiten u can check qus – andro Apr 12 '16 at 08:28
  • Well in my code it gives message that Method invocation cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) may produce NullPointerException. Not always produce NullPointerException. – Mrunal Apr 12 '16 at 08:32