0

I am trying to retrieve file path from URI. But my cursor is returning null.

Two problems:

  1. Uri may be audio/ video. So how can I retrieve file path.
  2. Why cursor is returning null?

Here is my code

    String[] proj = { MediaStore.Video.Media.DATA };
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    //here cursor is null. So i am getting Null pointer exception             
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);  

Permissions I have used:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
saa
  • 1,538
  • 2
  • 17
  • 35
  • check my answer. It gives absolute path of file from uri and is working on all android devices. – ELITE Sep 21 '16 at 15:14
  • [http://stackoverflow.com/questions/13209494/how-to-get-the-full-file-path-from-uri](http://stackoverflow.com/questions/13209494/how-to-get-the-full-file-path-from-uri) – Abderazak Amiar Sep 21 '16 at 15:42

2 Answers2

0
public void songList(){
    ContentResolver contentResolver = getContentResolver();
    Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor cur = contentResolver.query(uri, null, null, null, null);

    if(cur.moveToFirst()){
        do {
            int pathIndex = cur.getColumnIndex(MediaStore.Audio.Media.DATA);

            int nameIndex = cur.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);

            String spath = cur.getString(pathIndex);
            String name = cur.getString(nameIndex);     
            paths.add(spath.substring(4));
            songs.add(name);        
        } while (cur.moveToNext());

    }
Arya
  • 1,729
  • 3
  • 17
  • 35
0

You can use get File path from diffrent SDk versions

Use RealPathUtils for it

public class RealPathUtils {

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);

// 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 = 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();
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 get the file Path from URI

 String path = null;
            if (Build.VERSION.SDK_INT < 11)
                path =   RealPathUtils.getRealPathFromURI_BelowAPI11(MainActivity.this, uri);

                // SDK >= 11 && SDK < 19
            else if (Build.VERSION.SDK_INT < 19)
                path = RealPathUtils.getRealPathFromURI_API11to18(MainActivity.this, uri);

                // SDK > 19 (Android 4.4)
            else
                path = RealPathUtils.getRealPathFromURI_API19(MainActivity.this, uri);
            Log.d(TAG, "File Path: " + path);
            // Get the file instance
             File file = new File(path);
Harsh Bhavsar
  • 1,561
  • 4
  • 21
  • 39