内置摄像头,使用额外的 MediaStore.EXTRA_OUTPUT 存储图片两次(在我的文件夹中,在默认值中) [英] Built-in Camera, using the extra MediaStore.EXTRA_OUTPUT stores pictures twice (in my folder, and in the default)

查看:17
本文介绍了内置摄像头,使用额外的 MediaStore.EXTRA_OUTPUT 存储图片两次(在我的文件夹中,在默认值中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个使用内置相机的应用程序.我通过单击一个按钮来调用此代码段:

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很好存储在sdcard/myFolder/myPicture.jpg,但它也存储在/sdcard/DCIM/Camera/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.

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

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

我想我会直接使用 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) } );
}

此代码基于以下帖子:删除图库拍摄意图照片后的图像

这篇关于内置摄像头,使用额外的 MediaStore.EXTRA_OUTPUT 存储图片两次(在我的文件夹中,在默认值中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆