通过文件名获取uri

private Uri parsePath(String path) {
        String fullPath = "silent";
        if (!"".equals(path) && !"silent".equalsIgnoreCase(path)) {
            fullPath = Environment.getCustpackDirectory()
                    + "/audio/" + path;
        }
        Log.d(TAG, "fullPath = " + fullPath);
        Log.d(TAG, "uri = " + getUri(fullPath));
        return getUri(fullPath);
    }

    private Uri getUri(String path) {
        String uriString;
        Cursor internalCursor = getInternalRingtones(path);
        if (internalCursor != null) {
            if (internalCursor.getCount() > 0) {
                internalCursor.moveToFirst();
                uriString = MediaStore.Audio.Media.INTERNAL_CONTENT_URI
                        + "/"
                        + internalCursor.getString(internalCursor
                                .getColumnIndex("_id"));
                return Uri.parse(uriString);
            }
            internalCursor.close();
        }

        Cursor mediaCursor = getMediaRingtones(path);
        if (mediaCursor != null) {
            if (mediaCursor.getCount() > 0) {
                mediaCursor.moveToFirst();
                uriString = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
                        + "/"
                        + mediaCursor.getString(mediaCursor
                                .getColumnIndex("_id"));
                return Uri.parse(uriString);
            }
            mediaCursor.close();
        }
        return null;
    }

    private Cursor getInternalRingtones(String path) {
        return mContext.getContentResolver().query(
                MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
                new String[] {
                        MediaStore.Audio.Media._ID,
                        MediaStore.Audio.Media.DATA,
                        "\"" + MediaStore.Audio.Media.INTERNAL_CONTENT_URI
                                + "\"", MediaStore.Audio.Media.TITLE_KEY
                },
                "_data=?", new String[] {
                    path
                },
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    }

    private Cursor getMediaRingtones(String path) {
        final String status = Environment.getExternalStorageState();

        return (status.equals(Environment.MEDIA_MOUNTED) || status
                .equals(Environment.MEDIA_MOUNTED_READ_ONLY)) ? mContext
                .getContentResolver().query(
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        new String[] {
                                MediaStore.Audio.Media._ID,
                                MediaStore.Audio.Media.DATA,
                                MediaStore.Audio.Media.TITLE_KEY
                        }, "_data=?",
                        new String[] {
                            path
                        },
                        MediaStore.Audio.Media.DEFAULT_SORT_ORDER) : null;
    }
}


你可能感兴趣的:(String,null,Path)