照片没有出现在画廊

时间:2013-10-24 06:06:20

标签: android camera save gallery storage

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

            }
        }

3 个答案:

答案 0 :(得分:2)

尝试此处给出的代码: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值。

答案 1 :(得分:1)

这将适用于摄像头捕获活动结果中使用的所有Android版本

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

答案 2 :(得分: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);