2014년 5월 29일 목요일

Conversion between Uri and file path

1. Conversion of Uri to file path
public String getPathFromUri(Uri uri){
Cursor cursor = getContentResolver().query(uri, null, null, null, null );
cursor.moveToNext(); 
String path = cursor.getString( cursor.getColumnIndex( "_data" ) );
cursor.close();

return path;
}

2. Conversion of file path to Uri
public Uri getUriFromPath(String path)
String fileName= "file:///sdcard/DCIM/Camera/2013_07_07_12345.jpg";
Uri fileUri = Uri.parse( fileName );
String filePath = fileUri.getPath();
Cursor c = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
null, "_data = '" + filePath + "'", null, null );
cursor.moveToNext()
int id = cursor.getInt( cursor.getColumnIndex( "_id" ) );
Uri uri = ContentUris.withAppendedId( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id );

return uri;
}

댓글 없음:

댓글 쓰기