在android中使用相机意图获取拍摄图像的path

我一直在尝试获取捕获图像的path,以删除图像,发现了很多答案在stackoverflow但他们都没有为我工作,我得到了以下答案

private String getLastImagePath() { final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA }; final String imageOrderBy = MediaStore.Images.Media._ID + " DESC"; Cursor imageCursor = POS.this.getContentResolver().query( 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)); return fullPath; } else { return ""; } } 

但是这个代码工作在三星选项卡,但不能在联想标签和我球选项卡中工作。 所以,任何人都可以帮助我find其他解决scheme做同样的? 任何帮助将不胜感激。 谢谢。

这是我的onActivityResult

 if (requestCode == CmsInter.CAMERA_REQUEST && resultCode == RESULT_OK) { //Bitmap photo = null; //photo = (Bitmap) data.getExtras().get("data"); String txt = ""; if (im != null) { String result = ""; //im.setImageBitmap(photo); im.setTag("2"); int index = im.getId(); String path = getLastImagePath(); try { bitmap1 = BitmapFactory.decodeFile(path, options); bitmap = Bitmap .createScaledBitmap(bitmap1, 512, 400, false); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytData = baos.toByteArray(); try { baos.close(); } catch (IOException e) { e.printStackTrace(); } result = Base64.encode(bytData); bytData = null; } catch (OutOfMemoryError ooM) { System.out.println("OutOfMemory Exception----->" + ooM); bitmap1.recycle(); bitmap.recycle(); } finally { bitmap1.recycle(); bitmap.recycle(); } } 

像这样尝试

通过相机意图如下

 Intent intent = new Intent(this); startActivityForResult(intent, REQ_CAMERA_IMAGE); 

捕捉图像后如下所示写入一个OnActivityResult

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); knop.setVisibility(Button.VISIBLE); // CALL THIS METHOD TO GET THE URI FROM THE BITMAP Uri tempUri = getImageUri(getApplicationContext(), photo); // CALL THIS METHOD TO GET THE ACTUAL PATH File finalFile = new File(getRealPathFromURI(tempUri)); System.out.println(mImageCaptureUri); } } public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } public String getRealPathFromURI(Uri uri) { Cursor cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); return cursor.getString(idx); } 

并检查日志

有一个解决scheme来创build文件(在外部caching目录或任何其他地方),并把这个文件的uri作为输出额外的摄像机意图 – 这将定义所拍摄的图片将被存储的path。

这里是一个例子:

 File file; Uri fileUri; final int RC_TAKE_PHOTO = 1; private void takePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); file = new File(getActivity().getExternalCacheDir(), String.valueOf(System.currentTimeMillis()) + ".jpg"); fileUri = Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); getActivity().startActivityForResult(intent, RC_TAKE_PHOTO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_TAKE_PHOTO && resultCode == RESULT_OK) { //do whatever you need with taken photo using file or fileUri } } } 

那么如果你不再需要该文件,你可以使用file.delete();来删除它file.delete();

顺便说一下,当用户从应用程序设置中清除应用程序的caching时,caching目录中的文件将被删除。

试试这种方法来获取相机捕获的original图像的path。

 public String getOriginalImagePath() { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null); int column_index_data = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToLast(); return cursor.getString(column_index_data); } 

此方法将返回相机捕获的最后一个图像的path。 所以这个path将是原始图像而不是缩略图位图。

尝试这个

 String[] projection = { MediaStore.Images.Media.DATA }; @SuppressWarnings("deprecation") Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); int column_index_data = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); image_path = cursor.getString(column_index_data); Log.e("path of image from CAMERA......******************.........", image_path + ""); 

用于捕捉图像:

  String fileName = "temp.jpg"; ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, fileName); mCapturedImageURI = getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); values.clear();