【发布时间】:2015-07-01 19:52:59
【问题描述】:
我使用以下代码 sn-ps 来保存用户在设备上拍摄的图像:
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
我在这里做错了什么吗?还是只是三星没有遵循标准?
【问题讨论】:
-
你在模拟器上测试过吗?
-
它适用于模拟器以及除三星以外的所有设备。
-
存储路径将根据各种设备的内部或外部存储器而改变
-
“但图像保存在设备上的以下路径”——您如何确定这一点?您是在使用设备上的文件管理器,还是在使用设备外部的工具?
-
@CommonsWare 我只是用 adb shell 环顾四周。
标签: android android-file