我用下面的代碼
String pictures_path = Utils.getRealPathFromURI(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
File path = new File(pictures_path);
if (path.isFile())
path = path.getParentFile();
其中utils的:
public static String getRealPathFromURI(Uri content_uri, int media_type) {
String column = MediaType.MEDIA_TYPE_PICTURE;
ContentResolver content_resolver = getContext().getContentResolver();
String [] proj = { column };
Cursor cursor = content_resolver.query(content_uri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(column);
if (!cursor.moveToFirst())
return null;
return cursor.getString(column_index);
}
編輯:可惜的是,在上面的方法可能並不總是工作:(...
最後我做手工檢查:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
if (path.exists()) {
File test1 = new File(path, "100MEDIA/");
if (test1.exists()) {
path = test1;
} else {
File test2 = new File(path, "100ANDRO/");
if (test2.exists()) {
path = test2;
} else {
File test3 = new File(path, "Camera/");
if (!test3.exists()) {
test3.mkdirs();
}
path = test3;
}
}
} else {
path = new File(path, "Camera/");
path.mkdirs();
}
我認爲所有「近期」手機(v4.0 +)都使用「Camera」文件夾。 – 2014-02-26 17:39:01
@ AlikElzin-kilaka我的xperia z3和z5都v5.1.1商店在DCIM/100ANDRO FYI – 2016-01-18 01:50:00