内置相机,使用额外的MediaStore.EXTRA_OUTPUT将图片存储两次在我的文件夹中,默认情况下

问题描述

| 我目前正在开发使用内置相机的应用程序。 我通过单击按钮来调用代码段:
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类     

解决方法

        在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,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) } );
}
该代码基于以下内容:拍摄相机意图照片后删除图库图像     ,        虽然来自“ Ilango J \”的答案提供了基本思路。.我认为我实际上会写出实际的编写方式。 应该避免在intent.putExtra()中设置的临时文件路径,因为这是跨不同硬件的非标准方式。在HTC Desire(Android 2.2)上无法正常使用,而且我听说它可以在其他手机上使用。最好采用在任何地方都可以使用的中立方法。 请注意,此解决方案(使用Intent)要求手机的SD卡可用且未安装在PC上。将SD卡连接到PC时,即使是普通的Camera应用程序也无法使用。 1)启动相机拍摄意图。注意,我禁用了临时文件写入(在不同硬件上是非标准的)
    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);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
3)克隆并删除文件。看到我使用Uri \的InputStream读取内容。 也可以从“ 5”的文件中读取相同的内容。
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,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);
        }
    }
}
    ,        试试这个代码:
 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);