0

我正在尝试使用 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);

            }
        }
4

3 回答 3

2

尝试此处给出的代码:将照片添加到图库

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价值。

于 2013-10-24T06:13:13.503 回答
1

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

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

于 2015-12-25T12:07:17.673 回答
0

您需要为此设置一个值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); 
于 2013-10-24T06:13:17.423 回答