您的位置:首页 > 移动开发 > Android开发

android-取URI下file或Content对应的文件

2015-12-04 13:55 489 查看
* 通过Uri返回File文件 * 注意:通过相机的是类似content://media/external/images/media/97596 * 通过相册选择的:file:///storage/sdcard0/DCIM/Camera/IMG_20150423_161955.jpg

String schemeStr = data.getScheme().toString();

String picPath = "";

if (schemeStr.compareTo("file") == 0) {

picPath = uri.toString();

picPath = picPath.replace("file://", "");

} else if (schemeStr.compareTo("content") == 0) {

// 4.2.2以后

String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = null;

if (fragment != null) {

cursor = fragment.getActivity().getContentResolver()

.query(uri, filePathColumn, null, null, null);

} else {

cursor = activity.getContentResolver().query(uri,

filePathColumn, null, null, null);

}

if (cursor != null && cursor.moveToFirst()) {

int columnIndex = cursor

.getColumnIndex(filePathColumn[0]);

picPath = cursor.getString(columnIndex);

cursor.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: