从真正的路径获取Uri

时间:2011-10-30 12:53:19

标签: android

我有一个像“file:///mnt/sdcard/3dphoto/temp19.jps”这样的文件的真实路径 ,我怎么能得到像“content:// media / external / images / media / 1”这样的uri?

3 个答案:

答案 0 :(得分:6)

在文件路径中转换“file:// ...”,使用以下代码查找项目的ID,然后将其附加到提供程序URI。此外,基于文件扩展名,使用正确的提供程序(例如MediaStore.Video.Media.EXTERNAL_CONTENT_URI或MediaStore.Image.Media.EXTERNAL_CONTENT_URI)

/**
 * Given a media filename, returns it's id in the media content provider
 *
 * @param providerUri
 * @param appContext
 * @param fileName
 * @return
 */
public long getMediaItemIdFromProvider(Uri providerUri, Context appContext, String fileName) {
    //find id of the media provider item based on filename
    String[] projection = { MediaColumns._ID, MediaColumns.DATA };
    Cursor cursor = appContext.getContentResolver().query(
            providerUri, projection,
            MediaColumns.DATA + "=?", new String[] { fileName },
            null);
    if (null == cursor) {
        Log.d(TAG_LOG, "Null cursor for file " + fileName);
        return ITEMID_NOT_FOUND;
    }
    long id = ITEMID_NOT_FOUND;
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        id = cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID));
    }
    cursor.close();
    return id;
}

有时,在将一个媒体文件添加到设备的存储空间后,MediaProvider不会立即刷新。您可以使用此方法强制刷新其记录:

/**
 * Force a refresh of media content provider for specific item
 * 
 * @param fileName
 */
private void refreshMediaProvider(Context appContext, String fileName) {
    MediaScannerConnection scanner = null;
    try {
        scanner = new MediaScannerConnection(appContext, null);
        scanner.connect();
        try {
            Thread.sleep(200);
        } catch (Exception e) {
        }
        if (scanner.isConnected()) {
            Log.d(TAG_LOG, "Requesting scan for file " + fileName);
            scanner.scanFile(fileName, null);
        }
    } catch (Exception e) {
        Log.e(TAG_LOG, "Cannot to scan file", e);
    } finally {
        if (scanner != null) {
            scanner.disconnect();
        }
    }
} 

答案 1 :(得分:6)

我对我的文件浏览器活动有同样的问题...但你应该知道文件的contenturi只支持图像,音频和视频等媒体库数据....我正在给你从选择获取图像内容uri来自SD卡的图像......试试这段代码......可能会对你有用......

public static Uri getImageContentUri(Context context, File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.Media._ID },
                MediaStore.Images.Media.DATA + "=? ",
                new String[] { filePath }, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                return context.getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }

答案 2 :(得分:1)

另一种简单方法:

File file = new File(Path);

Uri uri = Uri.fromFile(file);