获取图像的路径从ACTION_IM​​AGE_CAPTURE意向路径、意向、图像、ACTION_IM

2023-09-11 12:29:21 作者:看你背影走

您好我现在用 ACTION_IM​​AGE_CAPTURE 使用捕捉图像意图如下:

Hi I am using ACTION_IMAGE_CAPTURE for capturing image using Intent as follows:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT, (new File(Environment.getExternalStorageDirectory(),
String.valueOf(System.currentTimeMillis()) + ".jpg"))
);
startActivityForResult(cameraIntent, 0);

我需要存储图像中的SD卡和检索图像使用 onActivityResult 法的路径。我不知道如何让拍摄图像的图像路径​​。

I need to store image in an sdcard and retrieve the path of that image using the onActivityResult method. I don't know how to get the image path of the currently captured image.

如果任何人知道,请帮助。

If any one knows please help.

感谢

推荐答案

这是它是如何工作的2.2(比previous版本有所不同)。当启动意图

This is how it works on 2.2 (different than on previous versions). When starting intent

        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);  
        startActivityForResult(intent, CAPTURE_PICTURE_INTENT);

您需要记住 mCapturedImageURI

当你拍摄的图像,在 onActivityResult()使用URI来获取文件路径:

When you capture image, in onActivityResult() use that URI to obtain file path:

            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);

更新:在新的Andr​​oid设备,您就不需要MediaStore.EXTRA_OUTPUT,但你更演绎出图像/视频URI从data.getData()从onActivityResult(...,意图数据)获得,因为在

UPDATE: On new Android devices you would not need MediaStore.EXTRA_OUTPUT, but you rather deduce image/video URI from data.getData() received from onActivityResult(..., Intent data), as nicely described under

的Andr​​oid ACTION_IM​​AGE_CAPTURE意图

然而,由于这部分是受到生产厂家的适应,你可能仍然会遇到手机,其中旧的方法可能是有用的。

However, since this part is subject to manufacturer adaptation, you may still encounter phones where "old" approach may be useful.