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

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

问题描述

我想用相机意图制作一张照片并将其保存到默认的 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.

我正在尝试使用以下代码:

I am trying it with the following code:

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

使用此代码,相机会打开,在我拍完一张照片后,它会关闭并将照片保存到默认图像文件夹(通常是/dcim/camera 或 sdcard/dcim/camera...)

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

等等

最近两天我研究了这个问题的解决方案,网上和 stackoverflow 上有很多文章,但没有任何效果.我不想要缩略图,我只想要相机拍摄的图像的路径(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?

推荐答案

图片将被存储两次,第一次存储在图库文件夹中,然后存储在您在 putExtra(MediaStore.EXTRA_OUTPUT, path) 方法中指定的文件中.

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

此示例基于以下帖子:删除图库图片拍摄意图照片后

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

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