【问题标题】:How to specify a directory for new photos in INTENT_ACTION_STILL_IMAGE_CAMERA如何在 INTENT_ACTION_STILL_IMAGE_CAMERA 中为新照片指定目录
【发布时间】:2018-06-20 08:53:31
【问题描述】:

我需要使用相机意图一次拍摄多张照片。用户将按下应用程序中的按钮,然后将使用相机应用程序。用户将拍摄多张照片。当用户返回主应用程序时,我需要将这些照片存储在单独的目录中。

就我所见,INTENT_ACTION_STILL_IMAGE_CAMERA 是最好的意图。是否可以为相机意图指定一个目录,以便相机将所有新照片(直到用户返回主应用程序)存储到该目录?这类似于使用EXTRA_OUTPUT 指定ACTION_IMAGE_CAPTURE 的文件路径。

【问题讨论】:

    标签: android android-intent android-camera android-camera-intent


    【解决方案1】:

    编辑:误读了有问题的意图。

    INTENT_ACTION_STILL_IMAGE_CAMERA 不带任何参数;你不能给它一个目录来保存图像,或者其他任何东西。它只是为了启动默认的相机应用程序而设计的,就像点击应用程序的主启动图标一样。

    它通常被小部件、锁屏或其他类似功能用来启动相机。

    还有ACTION_IMAGE_CAPTURE,它以特殊模式启动相机应用程序,在该模式下拍摄单张照片,然后返回到发出请求的应用程序。在那里,可以提供文件的目的地。但这种捕捉意图并不是为一次拍摄多张照片而设计的。

    您需要循环调用此 Intent,或者您需要构建自己的相机捕捉活动。

    【讨论】:

    • INTENT_ACTION_STILL_IMAGE_CAMERA 拍完一张照片后没有返回。
    【解决方案2】:

    一个适合我的解决方案是在启动相机之前保存所有照片路径+名称,然后在拍摄所有照片后比较列表。

    但更大的问题是读取 SD 卡路径,而在 Android 中读取内部存储很容易。

    【讨论】:

      【解决方案3】:

      首先,将图像存储为ArrayList,并为这些图像创建一个 ImageAdapter,例如在 GridView 中。其次,在 GridView 上设置setOnItemClickListener。 第三,创建一个Intent 来启动您的 ImageViewActivity(或您喜欢的任何名称),然后对这些图像(图像的 ID)执行一个 intent.putExtra(EXTRA_RES_ID)。最后,startActivity(intent)

      或者,根据developer.android.com

      以下是使用日期时间戳为新照片返回唯一文件名的方法中的示例解决方案:

      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 storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
          File image = File.createTempFile(
              imageFileName,  /* prefix */
              ".jpg",         /* suffix */
              storageDir      /* directory */
          );
      
          // Save a file: path for use with ACTION_VIEW intents
          mCurrentPhotoPath = image.getAbsolutePath();
          return image;
      }
      

      使用此方法可用于为照片创建文件,您现在可以像这样创建和调用 Intent:

      static final int REQUEST_TAKE_PHOTO = 1;
      
      private void dispatchTakePictureIntent() {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          // Ensure that there's a camera activity to handle the intent
          if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
              // Create the File where the photo should go
              File photoFile = null;
              try {
                  photoFile = createImageFile();
              } catch (IOException ex) {
                  // Error occurred while creating the File
                  ...
              }
              // Continue only if the File was successfully created
              if (photoFile != null) {
                  Uri photoURI = FileProvider.getUriForFile(this,
                                                        "com.example.android.fileprovider",
                                                        photoFile);
                  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                  startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
              }
          }
      }
      

      在此处查看更多信息Android Photo Basics

      【讨论】:

      • 一开始我没有任何图像。我只有在从相机应用程序返回后才能获得图像。在使用意图之前如何将图像存储在 ArrayList 中?
      猜你喜欢
      • 2019-05-18
      • 2018-11-10
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多