获取路径和文件名从相机意图的结果 [英] Get Path and Filename from Camera intent result

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

问题描述

我想要用相机意图画面并将其保存到默认的DCIM文件夹。然后,我想要得到的路径/文件名,其中的图片存储。

I want to make a picture with the camera intent and save it to the default DCIM folder. Then I want to get the path/filename where the picture is stored.

我想用下面的code:

I am trying it with the following code:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);

通过这个code,相机开启后,我采取了一个画面它关闭并保存图片的默认图像文件夹(通常是/ DCIM /摄像机或SD卡/ DCIM /摄像头......)

With this code, the camera opens and after I have taken one picture it closes and saves the picture to the default image folder (usually /dcim/camera or sdcard/dcim/camera...)

但我怎么能得到拍摄图片的路径和文件名呢? 我已经试过几乎所有的 onActivityResult 我试过

but how can I get the path and filename of the taken picture now? I have tried almost everything in onActivityResult I tried

String result = data.getData();

String result = data.getDataString();

String result = data.toURI();

Uri uri = data.getData();

等。

我研究了最近两天找到一个解决方案,也有在网站上和计算器,但没有工作的很多文章。 我不想缩略图,我只想路径(URI?),以该相机拍摄的图像。

I researched the last two days to find a solution for this, there are many articles on the web and on stackoverflow but nothing works. I don't want a thumbnail, I only want the path (uri?) to the image that the camera has taken.

感谢您的帮助

编辑: 当我尝试:

path=Environment.DIRECTORY_DCIM + "/test.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE); 

不图像保存为test.jpg放在但与正常图像的名字2011-10-03 ..... JPG(但这不要紧太多,我只需要在路径的形象,也没关系什么样的名字)。

it does not store the image as test.jpg but with the normal image name 2011-10-03.....jpg (but that is ok too, i only need the path to the image, it does not matter what the name is).

最好的问候

修改再次

path=Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
File file = new File(path,"test111111111.jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(intent, TAKE_PICTURE); 

当我尝试这一点,它存储的图像,以正确的文件夹,并用给定的名称(如test111111.jpg)。 但是,我怎么能现在得到的文件路径在onActivityResult?

When I try this, it stores the image to the right folder and with the given name (e.g. test111111.jpg). But how can I get the filepath now in onActivityResult?

推荐答案

图像将被存储两次,第一次在库文件夹,以后你especified上putExtra(MediaStore.EXTRA_OUTPUT,路径)方法的文件。

The picture will be stored twice, first on gallery folder, and after on the file you especified on putExtra(MediaStore.EXTRA_OUTPUT, path) method.

您可以获取采取这样做的最后一张照片:

You can obtain the last picture taken doing that:

/**
 * 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;
    }
}

该样品是基于岗位:<一href="http://stackoverflow.com/questions/6390163/deleting-a-gallery-image-after-camera-intent-photo-taken">Deleting后摄像头意图照片画廊影像拍摄

这篇关于获取路径和文件名从相机意图的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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