【问题标题】:Photo does not show up to gallery照片未显示在图库中
【发布时间】:2013-10-24 06:06:20
【问题描述】:

我正在尝试使用 android 相机拍照并告诉它保存到手机的图库中。我想我在路上搞砸了,但我似乎找不到我的错误。有人可以帮助我吗?我是android的新手。

调用相机的代码

   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  String uriToFileInExternalStorage = null;
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriToFileInExternalStorage);
                    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

处理照片的代码并告诉它去画廊。

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == CAMERA_PIC_REQUEST) {
                //check if camera has taken picture by checking request code

                Toast.makeText(MainActivity.this, "Photo Captured", Toast.LENGTH_SHORT).show();


                Uri mPath=data.getData();
                 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

                    mediaScanIntent.setData(mPath);
                    this.sendBroadcast(mediaScanIntent);

            }
        }

【问题讨论】:

标签: android camera save gallery storage


【解决方案1】:

试试这里给出的代码:Add the Photo to a Gallery

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);

我建议您下载示例 PhotoIntentActivity 以阅读并了解如何获取 mCurrentPhotoPath 值。

【讨论】:

  • 我累了,它给了我错误。 “mCurrentPhotoPath 无法解析为变量”
  • @user2872261 检查更新的代码。您正在做的错误是您没有使用实际路径名捕获图像。
  • 感谢您的回复。如何使用路径名捕获图像?我认为 captureImageUri 是路径名。如何获取 mCurrentPhotoPath 的字符串?我得到了 mCurrentPhotoPath 的空值...是什么给了 mCurrentPhotoPath 一个字符串?
  • 喜欢这个Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(mCurrentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(capturedImageUri); this.sendBroadcast(mediaScanIntent); ?
  • @user2872261 先试试
【解决方案2】:

这将适用于相机捕获活动结果中使用的所有 android 版本

MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);

【讨论】:

  • 谢谢,成功了!我不知道为什么,但是使用 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 发送广播的最常见方法对我不起作用(Nexus 5x,Nougat 7.1),但您的方法有所帮助!
【解决方案3】:

你需要给这个uriToFileInExternalStorage设置一个值

示例代码:

fileName = "image_" + String.valueOf(numImages) + ".jpg";

File output = new File(direct + File.separator + fileName); // create
                                                                    // output
while (output.exists()) { // while the file exists
    numImages++; // increment number of images
    fileName = "image_" + String.valueOf(numImages) + ".jpg";
    output = new File(outputFolder, fileName);
}

uriToFileInExternalStorage = Uri.fromFile(output); 

【讨论】:

  • 我不明白 :( 对不起,我是新手。我不知道从哪里开始......我应该把这个“Uri mPath=data.getData();”值放在哪里?
  • 不,这是您相机调用方法的更改。您已将其设置为 null。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
相关资源
最近更新 更多