Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 内置摄像头,使用extra MediaStore.extra_输出将图片存储两次(在我的文件夹中,默认情况下)_Android_Path_Camera_Image - Fatal编程技术网

Android 内置摄像头,使用extra MediaStore.extra_输出将图片存储两次(在我的文件夹中,默认情况下)

Android 内置摄像头,使用extra MediaStore.extra_输出将图片存储两次(在我的文件夹中,默认情况下),android,path,camera,image,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 +

我目前正在开发一款使用内置摄像头的应用程序。 我通过单击按钮来调用此代码段:

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类

试试下面的代码:

 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);
而“Ilango J”的答案提供了基本思路。。我想我应该写下我是如何做到的。 我们在intent.putExtra()中设置的临时文件路径应该避免,因为它是跨不同硬件的非标准方式。在HTC Desire(安卓2.2)上,它不起作用,我听说它在其他手机上也能起作用。最好采用中性的方法,在任何地方都有效

请注意,此解决方案(使用Intent)要求手机的SD卡可用且未安装到电脑上。当SD卡连接到电脑时,即使是普通的摄像头应用程序也无法工作

1) 启动相机拍摄意图。注意,我禁用了临时文件写入(跨不同硬件的非标准)

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

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

另一种在android 2.1上测试的方法是,获取gallery最后一张图像的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) } );
}

此代码基于帖子:

这与上面给出的源代码有何不同。毕竟,常量
MediaStore.EXTRA_OUTPUT
解析为字符串
“OUTPUT”
toodo一件事在捕获图像后删除myPicture.jpg图像。在我的android galaxy S II上使用file.delete()function.activity结果数据为空是的,三星制造的手机存在问题。我会在找到解决方法后尽快发布方法。我刚订购了两款Nexus S手机,它们也有三星相机的问题。。用谷歌搜索。。其他人也面临着这个问题。这是可行的,但当拍摄照片时,元数据会在MediaStorage中注册,所以当你去浏览图库时,即使文件不见了,照片仍然会在那里。你如何删除它?很抱歉,我应该补充说这是Desire 2.2上的。它工作正常。但在我的情况下,它会从SD卡文件夹中删除图像。我使用的是索尼爱立信手机。在我的应用程序中,我会捕获图像并保存到SD卡文件夹,然后返回到我添加了网格视图的活动,该活动从SD卡获取图像并添加到网格视图。你的代码正常吗很好,但它会从我的文件夹中删除。任何帮助都是值得的。@AshishMishra我不确定我是否理解你的意思。如果你不想删除,就不要调用removeImage。@Derzu你不认为它会删除错误的图像,其中没有重复的图像吗created@FatalError对如果新映像未真正保存,则存在此风险。@Derzu是否有任何过程可以知道哪个设备创建了重复映像,而哪个设备无法保持复制映像。……thnks
/**
 * 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) } );
}