内置摄像头,使用额外的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将图片存储在默认文件夹中?

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

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

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

此代码基于以下post: 在拍摄相机意图照片后删除图库图像

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

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

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, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } 

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

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

试试这段代码:

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