【问题标题】:camera image not showing recently taken photo to select in Camera folder相机图像未显示最近拍摄的照片以在“相机”文件夹中选择
【发布时间】:2018-01-11 15:02:16
【问题描述】:

我正在使用 Android 7.0 移动设备进行测试。

我正在实现一个 android 应用程序。 我需要从设备相机拍摄照片,然后从应用程序使用的相机文件夹/图库中选择它。

但是在拍照并返回我的应用程序并尝试通过我的应用程序选择拍摄的照片后,当移动到相机文件夹时,最近拍摄的照片不在图库中。

但我注意到,当我拍摄另一张照片并通过我的应用再次从相机文件夹中选择该照片时,我可以看到我之前拍摄的照片,但看不到最近拍摄的照片。

我遵循了这个答案 - Android: Refreshing the Gallery after saving new images但这对我不起作用

如果有人可以回复答案并且该答案包含要查找的文件路径,请同时添加这些详细信息。例如:-“如何采用 android 相机图像库路径”

我在这里使用的代码:-

    val cursor: Cursor? = contentResolver.query(uri, projectionColumns, null, null, sortOrder)
    if (cursor != null && cursor.moveToFirst()) {
        while (cursor.moveToNext()) {
            // Get values per item
            val imageId = cursor.getString(cursor.getColumnIndex(projectionColumns[0]))
            val imageName = cursor.getString(cursor.getColumnIndex(projectionColumns[1]))
            val imagePath = cursor.getString(cursor.getColumnIndex(projectionColumns[2]))
            val dateTaken = cursor.getString(cursor.getColumnIndex(projectionColumns[3]))
            val imageSize = cursor.getString(cursor.getColumnIndex(projectionColumns[4]))
            val bucketId = cursor.getString(cursor.getColumnIndex(projectionColumns[5]))
            val bucketName = cursor.getString(cursor.getColumnIndex(projectionColumns[6]))

【问题讨论】:

  • 请提供一个minimal reproducible example,显示您用于“拍照”和“选择[ing]我的应用拍摄的照片”的代码。
  • 如果您转到我在问题中已经提供的链接,我想您可能会理解我在这里使用的代码。
  • 该链接没有显示我建议您添加到问题中的任何代码。
  • @CommonsWare 完成,添加了我调用图像的代码部分
  • 这没有显示您用于“拍照”和“选择[ing]我的应用程序捕获的照片”的代码。

标签: android android-camera android-image


【解决方案1】:

请注意,如果您在外部或内部私有目录中的某处为意图提供 Uri 或位置以在单击后存储图像图片,那么您需要广播 手动或尝试将其移动到另一个文件夹。

这是通过传递文件路径来广播它的代码。

public void broadCastMedia(Context context, String path) {
    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File file = new File(path);
    intent.setData(Uri.fromFile(file));
    context.sendBroadcast(intent);
}

这里是如何将一个复制到另一个位置说图片目录然后你需要创建一个临时文件到图片目录和一个原始文件并传递给这个。

//temp file
    File destFile = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "image.jpg");
//original file    
    File sourceFile = new File(uri.getPath());

     public static Boolean copyFile(File sourceFile, File destFile)
          throws IOException {
        if (!destFile.exists()) {
          destFile.createNewFile();

          FileChannel source = null;
          FileChannel destination = null;
          try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
          } finally {
            if (source != null)
              source.close();
            if (destination != null)
              destination.close();
          }
          return true;
        }
        return false;
      }

如果你在意图中给出了EXTRA_OUTPUT,那么你需要存储你传递的Uri,因为onActivityResult()中的数据意图将是null,那时你可以使用存储的Uri 对存储的文件执行任何操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    相关资源
    最近更新 更多