【问题标题】:Why aren't images taken using a camera intent saved to my phone?为什么不使用相机意图拍摄的图像保存到我的手机?
【发布时间】:2018-04-08 13:47:05
【问题描述】:

编辑

我的函数现在看起来像这样:

p

【问题讨论】:

  • 我应该在哪里使用这个功能?在 startActivityForResult(callCameraApplicationIntent, REQUEST_TAKE_PHOTO);之前?
  • 否,因为照片尚未拍摄。如果照片似乎是在您指定的位置拍摄的,请在 onActivityResult() 上执行此操作。
  • 如果 photoFile 设置在另一个函数中,我将如何使用它作为参数?
  • 在活动的某个字段中保持它。此外,请务必将其保存为已保存实例状态Bundle 的一部分,因为当相机应用程序处于前台时,您的进程可能会终止。 This sample app 演示了该技术,尽管我使用 File 代替 ACTION_VIEW 而不是 MediaScannerConnection。

标签: android android-camera-intent


【解决方案1】:

如果您想将图像保存在外部存储中,请使用 Externalstoragepublicdirectory 函数。

【讨论】:

    【解决方案2】:

    此意图需要 Uri 格式的 EXTRA_OUTPUT 位置,请参阅https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE。某些设备可能会理解绝对文件路径,但这不是记录在案的行为。

    您可以使用getExternalMediaDirs() 来避免许多权限限制。

    【讨论】:

      【解决方案3】:

      我对你的代码做了一些修改:

      将此添加到您的清单中的应用程序级别:

      <provider
              android:name="android.support.v4.content.FileProvider"
              android:authorities="com.example.android.fileprovider"
              android:exported="false"
              android:grantUriPermissions="true">
              <meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/file_paths" />
          </provider>
      

      然后创建file_paths.xml文件:

      <paths xmlns:android="http://schemas.android.com/apk/res/android">
          <external-cache-path name="my_cache" path="." />
          <external-path name="my_images" path="Pictures/" />
           </paths>
      

      然后在你的代码中:

        private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider";
      
        private String mTempPhotoPath;
      
       public void takePhoto() {
      // Create the capture image intent
          Intent imageCapture= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      
          // Ensure that there's a camera activity to handle the intent
          if (imageCapture.resolveActivity(getPackageManager()) != null) {
              // Create the temporary File where the photo should go
              File photoFile = null;
              try {
                  photoFile = createTempImageFile(this);
              } catch (IOException ex) {
                  // Error occurred while creating the File
                  ex.printStackTrace();
              }
              // Continue only if the File was successfully created
              if (photoFile != null) {
      
                  // Get the path of the temporary file
                  mTempPhotoPath = photoFile.getAbsolutePath();
      
                  // Get the content URI for the image file
                  Uri imageUri = FileProvider.getUriForFile(this,
                          FILE_PROVIDER_AUTHORITY,
                          photoFile);
      
                  // Add the URI so the camera can store the image
                  imageCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
          }
      
          static File createTempImageFile(Context context) throws IOException {
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                  Locale.getDefault()).format(new Date());
          String imageFileName = "JPEG_" + timeStamp + "_";
          File storageDir = context.getExternalCacheDir();
      
          return File.createTempFile(
                  imageFileName,  /* prefix */
                  ".jpg",         /* suffix */
                  storageDir      /* directory */
          );
      }
      
      
      //To save image
      
      String mCurrentPhotoPath = null;
      private File createImageFile() throws IOException {
      
          // Create an image file name
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
          String imageFileName = "IMG_" + timeStamp + "_";
          File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/DCIM/Camera");
          Log.d(TAG, "createImageFile: Saving image to:" + storageDir);
          File image = new File(storageDir, imageFileName);
      
          // Save a file: path for use with ACTION_VIEW intents
          mCurrentPhotoPath = image.getAbsolutePath();
      
          //Return the saved image
          return mCurrentPhotoPath ;
      } 
      

      【讨论】:

      • 此函数用于创建图像存储的文件名。我已经有一个函数来获取最后存储的图像的文件路径。
      • 这很好,但你知道我可以如何设置 file_paths.xml 以便我可以将新图像保存在 DCIM/Camera 文件夹中,因为应用程序的一部分会从该文件夹中获取所有图像.我可以将 'path="Pictures/" ' 替换为 'DCIM/Camera/' 吗?
      • 我已经尝试过了,但出现错误 - java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/DCIM/Camera/IMG_20180409_190420_896248155.jpg跨度>
      • 不,将Environment.DIRECTORY_PICTURES 替换为Environment.DIRECTORY_DCIM
      猜你喜欢
      • 2013-06-29
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      相关资源
      最近更新 更多