Android之uri、file、path相互转化

1、uri转file:

file = new File(new URI(uri.toString()));  

file转uri:

URI uri = file.toURI();  

2、uri转path:

private String getPath(Context context, Uri uri) {  
        String path = null;
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor == null) {
            return null;
        }
        if (cursor.moveToFirst()) {
            try {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        cursor.close();
        return path;
 }  

path转uri:

Uri uri = Uri.parse(path);  

3、file转path:

String path = file.getPath()  

path转file:

File file = new File(path)  

你可能感兴趣的:(Android)