如何从uri获取文件路径?

时间:2014-02-19 07:02:17

标签: android

我正在尝试从URI检索文件路径。但我的光标返回null。

两个问题:

  1. Uri可能是音频/视频。那么我该如何检索文件路径呢。
  2. 为什么游标返回null?
  3. 这是我的代码

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

    我使用过的权限:

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

2 个答案:

答案 0 :(得分: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());

    }

答案 1 :(得分:0)

您可以使用来自不同SDK版本的获取文件路径

使用 RealPathUtils

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

现在从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);