三星设备 returns 图像文件路径错误
Samsung device returns wrong path for Image file
我使用以下代码片段保存用户在设备上拍摄的图像:
private void createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
image.mkdirs();
// Save a file: path for use with ACTION_VIEW intents
cameraFilePath = "file://" + image.getAbsolutePath();
}
private void saveImageToGallery(Context context) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.parse(cameraFilePath));
context.sendBroadcast(mediaScanIntent);
}
这对大多数设备都适用,但一些三星(Galaxy S5、S3)的行为很奇怪;我的方法 createImageFile 创建以下路径:
file:///storage/emulated/0/Pictures/JPEG_20150701_131442_-1853613631.jpg
但图像保存在设备上的以下路径中:
/storage/emulated/legacy/Pictures/JPEG_20150701_131442_-1853613631.jpg
和
/storage/sdcard0/Pictures/JPEG_20150701_114009_-1853613631.jpg
我在这里做错了什么吗?或者只是三星没有遵循标准?
通过 getExternalStoragePublicDirectory()
等方法返回到您的进程的路径不必与您从您自己的应用之外的地方看到的一致。 Android 4.2+ 平板电脑和 Android 5.0+ 手机支持多个用户。每个用户都有自己独立的目录集,代表内部和外部存储。因此,您的进程获取将映射到 "real" 文件系统路径的路径,无论用户碰巧使用您的应用程序。该映射过程的详细信息是特定于制造商和设备的。
IOW,不用担心差异。如果您的应用程序读写正常,并且您能够从外部工具中找到您正在读写的文件以进行调试,那么一切都很好。
我使用以下代码片段保存用户在设备上拍摄的图像:
private void createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
image.mkdirs();
// Save a file: path for use with ACTION_VIEW intents
cameraFilePath = "file://" + image.getAbsolutePath();
}
private void saveImageToGallery(Context context) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.parse(cameraFilePath));
context.sendBroadcast(mediaScanIntent);
}
这对大多数设备都适用,但一些三星(Galaxy S5、S3)的行为很奇怪;我的方法 createImageFile 创建以下路径:
file:///storage/emulated/0/Pictures/JPEG_20150701_131442_-1853613631.jpg
但图像保存在设备上的以下路径中:
/storage/emulated/legacy/Pictures/JPEG_20150701_131442_-1853613631.jpg
和
/storage/sdcard0/Pictures/JPEG_20150701_114009_-1853613631.jpg
我在这里做错了什么吗?或者只是三星没有遵循标准?
通过 getExternalStoragePublicDirectory()
等方法返回到您的进程的路径不必与您从您自己的应用之外的地方看到的一致。 Android 4.2+ 平板电脑和 Android 5.0+ 手机支持多个用户。每个用户都有自己独立的目录集,代表内部和外部存储。因此,您的进程获取将映射到 "real" 文件系统路径的路径,无论用户碰巧使用您的应用程序。该映射过程的详细信息是特定于制造商和设备的。
IOW,不用担心差异。如果您的应用程序读写正常,并且您能够从外部工具中找到您正在读写的文件以进行调试,那么一切都很好。