file path return null always in lollipop android

Gopal Singh picture Gopal Singh · Mar 19, 2015 · Viewed 17.2k times · Source

this is my code when i'm getting image from internal storage (gallery). In lollipop file path return always null.

if (requestCode == PICK_IMAGE)  {
        if(resultCode == RESULT_OK){
            //image successfully picked
            // launching upload activity
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            columnindex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            file_path = cursor.getString(columnindex);
            Log.d(getClass().getName(), "file_path"+file_path);
            fileUri = Uri.parse("file://" + file_path);
            cursor.close();
            launchUploadActivity(true, PICK_IMAGE);
        }else if (resultCode == RESULT_CANCELED) {
            // user cancelled recording
            Toast.makeText(getApplicationContext(),"User cancelled image  selection", Toast.LENGTH_SHORT).show();
        } else {
            // failed to record video
            Toast.makeText(getApplicationContext(),"Sorry! failed to pick image", Toast.LENGTH_SHORT).show();
        }

Answer

Gopal Singh picture Gopal Singh · Mar 20, 2015

Thanx all,I found the solution.

    Uri selectedImage = data.getData();
            String wholeID = DocumentsContract.getDocumentId(selectedImage);

            // Split at colon, use second item in the array
            String id = wholeID.split(":")[1];

            String[] column = { MediaStore.Images.Media.DATA };     

            // where id is equal to             
            String sel = MediaStore.Images.Media._ID + "=?";

            Cursor cursor = getContentResolver().
                                      query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                      column, sel, new String[]{ id }, null);

            String filePath = "";

            int columnIndex = cursor.getColumnIndex(column[0]);

            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }   
            cursor.close();
            setImageFromIntent(filePath);