内置相机,使用额外MediaStore.EXTRA_OUTPUT将照片存储两次(在我的文件夹,并在默认)我的、两次、并在、文件夹

2023-09-12 22:46:10 作者:不抵你乐意

我目前正在开发一个应用程序,它使用内置的摄像头。 我把这个片段通过点击一个按钮:

I'm currently developing an app which uses the built-in Camera. I call this snippet by clicking a button :

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是以及存储在SD卡/ MyFolder中/ myPicture.jpg,但它的也存储在/ SD卡/ DCIM /相机/ 2011-06-14 10.36.10.jpg,这是默认的路径。

After taking the picture with the camera, the jpg is well stored in sdcard/myFolder/myPicture.jpg, but it is also stored in /sdcard/DCIM/Camera/2011-06-14 10.36.10.jpg, which is the default path.

有没有一种方法,以prevent内置相机存储图片的默认文件夹?

Is there a way to prevent the built-in Camera to store the picture in the default folder ?

非常感谢你的任何想法。

Thank you very much for any idea.

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

Edit : I Think I will use the Camera class directly

推荐答案

另一种方式,在Android 2.1的测试,是把ID或画廊最后一个图像的绝对路径,那么你就可以删除重复的图像。

Another way, tested on android 2.1, is take the ID or Absolute path of the gallery last image, then you can delete the duplicated image.

可以这样做:

/**
 * 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) } );
}

这code的立足岗位:Deleting后摄像头意图照片画廊影像拍摄

This code was based on the post: Deleting a gallery image after camera intent photo taken

 
精彩推荐
图片推荐