相机意图:图库中缺少图片

时间:2014-08-20 09:50:35

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

我有一个启动相机意图的应用程序,以便用户可以拍照。拍摄照片后,它将保存在特定文件夹中的外部存储器中。问题是我无法在图库中看到它。你有什么建议吗?我应该在onActivityResult()中做些什么吗?如果有,那是什么?

这是代码(我使用了android docs中的代码):

public void takePicture() {
        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) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

@SuppressLint("SimpleDateFormat")
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    String imageFileName = "IMG_" + timeStamp + "_";

    File imageFile = new File(Environment.getExternalStorageDirectory()
            + "/LocalSin");

    if (!imageFile.exists()) {
        imageFile.mkdirs();
    }

    File image = File.createTempFile(imageFileName, ".jpg", imageFile);

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

1 个答案:

答案 0 :(得分:0)

拍照后使用此代码。

public void addPicInGalery()
{
    Intent scan= new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File file=new File(path); // destination path where u want to store image
    Uri uri=Uri.fromFile(file);
    scan.setData(uri);
    getActivity().sendBroadcast(scan);
}