【问题标题】:open camera and saving image using FileProvider not working. getting Error: W/ContextImpl: Failed to ensure directory:打开相机并使用 FileProvider 保存图像不起作用。出现错误:W/ContextImpl:无法确保目录:
【发布时间】:2016-09-08 19:30:05
【问题描述】:

我使用以下链接将相机集成到我的应用中

https://developer.android.com/training/camera/photobasics.html#TaskScalePhoto

我按照每个步骤进行操作,但每次尝试从相机拍照时都会出现以下错误,并且还会弹出一个对话框,显示“不幸的是,相机已停止。

错误:

W/ContextImpl:无法确保目录:/storage/extSdCard/Android/data/com.example.daf.formvalidator/files/Pictures

E/OpenGLRenderer: SFEffectCache:clear(), mSize = 0

让我感到奇怪的是,以下代码(取自上面的链接)在 storageDir 的值中返回“/emulated/0” 以上错误显示“extSdCard”

// 返回 /storage/emulated/0/Android/data/com.example.daf.formvalidator/files/Pictures/

File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);

我想要什么?

-打开相机应用。

-将拍摄的照片存储到应用的私人存储,如链接中所述。

-想使用 FileProvider

我还尝试在意图上设置标志,例如:

takePictureIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

我找不到问题,花了将近 1 天的时间才找到问题。 请提供任何帮助。将不胜感激。

为什么“storageDir 的值”和错误信息有区别?

如果问题不清楚,请提出...

【问题讨论】:

    标签: android camera


    【解决方案1】:

    尝试将此权限添加到您的清单中:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 google 开发人员的示例,请尝试将您的文件放入自定义目录:

      String mCurrentPhotoPath;
      
      private File createImageFile() throws IOException {
          // Create an image file name
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
          String imageFileName = "JPEG_" + timeStamp + "_";
          File externalStorageDirectory = Environment.getExternalStorageDirectory();
              File directory = new File(externalStorageDirectory.getAbsolutePath() + "/yourappname");
              //create directory if not exist
              if (!directory.isDirectory()) {
                  directory.mkdirs();
              }
          File image = File.createTempFile(
              imageFileName,  /* prefix */
              ".jpg",         /* suffix */
              storageDir      /* directory */
          );
      
          // Save a file: path for use with ACTION_VIEW intents
          mCurrentPhotoPath = "file:" + image.getAbsolutePath();
          return image;
      }
      

      【讨论】:

      • 但我希望使用 FileProvider 将图像存储在应用程序的私有区域中。
      【解决方案3】:

      所以你想:

      打开相机应用。

      将拍摄的照片存储到链接中提到的应用的私人存储中。

      想使用 FileProvider

      我要提到的第一件事是,您发布的错误根本不是错误。这是一个警告,您应该可以忽略它。我认为该警告意味着他们无法判断目录是否存在(这与我稍后将要讨论的链接有关)。您可以为您的异常发布实际的堆栈跟踪吗?听起来这不是你的问题。

      您的帖子中接下来不清楚的是,您似乎混淆了私人存储和 SD 卡。根据定义,SD 卡上的文件不是私人存储。有关您的选项的详细说明,请参见此处:https://developer.android.com/guide/topics/data/data-storage.html#filesInternal

      具体来说,您需要注意的是,您正在做的是保存到 SD 卡,根据定义,它不是内部(或私有)存储。

      就“/storage/emulated/0/”和“/storage/sdcard/”之间的区别而言,它们应该都指向同一个目录。简短的版本是从 Android L 开始,Android 支持多用户。 “emulated/0”只是一个指向主用户存储目录的链接。这里让我感兴趣的是,通常当我听到“emulated/0”时,它是在内部存储的上下文中,但我不明白为什么它也无法链接到 SD 卡。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-05
        • 2019-12-17
        • 1970-01-01
        • 2017-04-24
        • 2014-11-12
        • 1970-01-01
        相关资源
        最近更新 更多