【问题标题】:Samsung device returns wrong path for Image file三星设备返回错误的图像文件路径
【发布时间】: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


【解决方案1】:

通过getExternalStoragePublicDirectory() 之类的方法返回到您的进程的路径不必与您在自己的应用程序之外的地方看到的内容一致。 Android 4.2+ 平板电脑和 Android 5.0+ 手机支持多用户。每个用户都有自己独立的一组目录,代表内部和外部存储。因此,无论用户碰巧使用您的应用程序,您的进程都会获取将映射到“真实”文件系统路径的路径。该映射过程的详细信息因制造商和设备而异。

IOW,不要担心差异。如果您的应用程序读写正常,并且您能够从外部工具中找到您正在读写的文件以进行调试,那么一切都很好。

【讨论】:

  • 感谢您的回答。问题是我需要使用该文件路径来显示保存的毕加索图像,毕加索不会找到/显示该文件路径的图像。
  • @Ascorbin:毕加索可以在getExternalStoragePublicDirectory() 中找到文件就好了。如果您遇到问题,请提出一个单独的 Stack Overflow 问题,在其中解释如何将图像放入getExternalStoragePublicDirectory(),如何尝试使用 Picasso 从getExternalStoragePublicDirectory() 读取这些图像,以及您有什么具体问题相遇。
  • 好的,谢谢。也许你可以看看这个新问题? stackoverflow.com/questions/31242161/…
猜你喜欢
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 2018-05-26
相关资源
最近更新 更多