从 ACTION_IMAGE_CAPTURE Intent 获取图像路径 [英] Get Path of image from ACTION_IMAGE_CAPTURE Intent

查看:19
本文介绍了从 ACTION_IMAGE_CAPTURE Intent 获取图像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 ACTION_IMAGE_CAPTURE 来使用 Intent 捕获图像,如下所示:

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

我需要将图像存储在 sdcard 中并使用 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 上的工作方式(与以前的版本不同).开始意图时

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

更新:在新的 Android 设备上,您不需要 MediaStore.EXTRA_OUTPUT,而是从 onActivityResult(..., Intent data) 收到的 data.getData() 中推断图像/视频 URI,正如在

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

Android ACTION_IMAGE_CAPTURE Intent

但是,由于这部分取决于制造商的调整,您仍然可能会遇到旧"方法可能有用的手机.

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

这篇关于从 ACTION_IMAGE_CAPTURE Intent 获取图像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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