我用下面的代码片段保存图像用户已在设备上:三星设备返回图像文件路径错误
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);
}
这对于大多数设备工作正常,一些三星(银河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
我在这里做一些基本错误?还是仅仅三星没有遵循标准?
你在模拟器上测试吗? –
它适用于仿真器以及除三星以外的所有设备。 – FWeigl
根据各种设备上的内部或外部存储器,存储路径将发生变化 –