【Androidでデータやファイルと戯れる】URIとファイルパスの相互変換
今回は「URI」から「ファイルシステム」のパスの相互変換する方法についてのメモ
「ファイルシステム」→「URI」の変換
File file = new File("");
Uri uri = Uri.fromFile(file);
「URI」→「ファイルシステム」の変換
String scheme = uri.getScheme();
String path = "";
if ("file".equals(scheme)) {
path = uri.getPath();
} else if("content".equals(scheme)) {
ContentResolver contentResolver = getApplicationContext().getContentResolver();
Cursor cursor = contentResolver.query(uri, new String[] { MediaStore.MediaColumns.DATA }, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
path = cursor.getString(0);
cursor.close();
}
}