内置摄像头,使用额外的MediaStore.EXTRA_OUTPUT存储图片两次(在我的文件夹中,默认情况下)

时间:2011-06-14 09:04:10

标签: android path camera image

我目前正在开发一款使用内置相机的应用。 我通过点击按钮来调用此代码段:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/myFolder/myPicture.jpg";
File file = new File( path );
//file.mkdirs();
Uri outputFileUri = Uri.fromFile( file );
//String absoluteOutputFileUri = file.getAbsolutePath();

intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);

使用相机拍照后,jpg在sdcard / myFolder / myPicture.jpg中存储,但还存储在/ sdcard / DCIM /中Camera / 2011-06-14 10.36.10.jpg,这是默认路径。

有没有办法阻止内置Camera将图片存储在默认文件夹中?

编辑:我想我会直接使用Camera类

3 个答案:

答案 0 :(得分:12)

另一种方法,在Android 2.1上测试,是获取图库最后一张图像的ID或绝对路径,然后你可以删除重复的图像。

可以这样做:

/**
 * Gets the last image id from the media store
 * @return
 */
private int getLastImageId(){
    final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
    Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
    if(imageCursor.moveToFirst()){
        int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
        String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        Log.d(TAG, "getLastImageId::id " + id);
        Log.d(TAG, "getLastImageId::path " + fullPath);
        imageCursor.close();
        return id;
    }else{
        return 0;
    }
}

并删除文件:

private void removeImage(int id) {
   ContentResolver cr = getContentResolver();
   cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );
}

此代码基于帖子:Deleting a gallery image after camera intent photo taken

答案 1 :(得分:9)

虽然“Ilango J”的答案提供了基本的想法。我想我实际上是在写我是如何实际做到的。 我们在intent.putExtra()中设置的临时文件路径应该避免,因为它是跨不同硬件的非标准方式。在HTC Desire(Android 2.2)上它没有用,而且我听说它适用于其他手机。最好采用一种适用于所有地方的中立方法。

请注意,此解决方案(使用意图)要求手机的SD卡可用且未安装到PC上。当SD卡连接到PC时,即使是普通的相机应用也无法工作。

1)启动Camera Capture意图。注意,我禁用了临时文件写入(不同硬件之间的非标准)

    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camera , 0);

2)处理回调并从Uri对象中检索捕获的图片路径并将其传递给步骤#3

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case CAPTURE_PIC: {
        if (resultCode == RESULT_OK && data != null) {
            Uri capturedImageUri = data.getData();
            String capturedPicFilePath = getRealPathFromURI(capturedImageUri);
            writeImageData(capturedImageUri, capturedPicFilePath);
            break;
        }
    }
    }
}

public String getRealPathFromURI(Uri contentUri) {
    String[] projx = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, projx, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

3)克隆并删除文件。看到我使用Uri的InputStream来读取内容。 同样可以从capturedPicFilePath的文件中读取。

public void writeImageData(Uri capturedPictureUri, String capturedPicFilePath) {

    // Here's where the new file will be written
    String newCapturedFileAbsolutePath = "something" + JPG;

    // Here's how to get FileInputStream Directly.
    try {
        InputStream fileInputStream = getContentResolver().openInputStream(capturedPictureUri);
        cloneFile(fileInputStream, newCapturedFileAbsolutePath);
    } catch (FileNotFoundException e) {
        // suppress and log that the image write has failed. 
    }

    // Delete original file from Android's Gallery
    File capturedFile = new File(capturedPicFilePath);
    boolean isCapturedCameraGalleryFileDeleted = capturedFile.delete();
}

  public static void cloneFile(InputStream currentFileInputStream, String newPath) {
    FileOutputStream newFileStream = null;

    try {

        newFileStream = new FileOutputStream(newPath);

        byte[] bytesArray = new byte[1024];
        int length;
        while ((length = currentFileInputStream.read(bytesArray)) > 0) {
            newFileStream.write(bytesArray, 0, length);
        }

        newFileStream.flush();

    } catch (Exception e) {
        Log.e("Prog", "Exception while copying file " + currentFileInputStream + " to "
                + newPath, e);
    } finally {
        try {
            if (currentFileInputStream != null) {
                currentFileInputStream.close();
            }

            if (newFileStream != null) {
                newFileStream.close();
            }
        } catch (IOException e) {
            // Suppress file stream close
            Log.e("Prog", "Exception occured while closing filestream ", e);
        }
    }
}

答案 2 :(得分:0)

试试这段代码:

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/myFolder/myPicture.jpg";
File file = new File( path );
//file.mkdirs();
Uri outputFileUri = Uri.fromFile( file );
//String absoluteOutputFileUri = file.getAbsolutePath();

intent.putExtra("output", outputFileUri);
startActivityForResult(intent, 0);