content:// scheme 和 file:// scheme

/** * 根据file path 获取content:// * @param context * @param filePath * @return content:// */ public static Uri getImageContentUri(Context context, String filePath) { 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/file”); return Uri.withAppendedPath(baseUri, “” + id); } else { File file = new File(filePath); if (file.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; } } }

相关文章
相关标签/搜索