如何从相机意图图像路径? [英] How to get image path from camera intent?

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

问题描述

我与Android 2.1的工作,我想从相机意图的结果真实路径。我读<一href="http://stackoverflow.com/questions/4184951/get-path-of-image-from-action-image-capture-intent">Get从ACTION_IM​​AGE_CAPTURE意图图像的路径,但它是为Android 2.2。

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据)
{
    如果(要求code == CAMERA_RESULT)
    {
        。位图的缩略图=(位图)data.getExtras()获得(数据);
        imv.setImageBitmap(缩略图);
         乌里selectedImageUri = data.getData();
         字符串路径= getRealPathFromURI(selectedImageUri);
    }
}

私人字符串getRealPathFromURI(URI contentUri)
{
    尝试
    {
        的String []凸出= {MediaStore.Images.Media.DATA};
        光标光标= managedQuery(contentUri,凸出,NULL,NULL,NULL);
        INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        返回cursor.getString(Column_Index中);
    }
    赶上(例外五)
    {
        返回contentUri.getPath();
    }
}
 

解决方案
  

它上面code工作在一些手机,但三星手机不能正常工作在我的情况,所以我实现的共同逻辑的所有设备。

当我捕捉摄像头的照片,所以我实现逻辑使用光标和迭代的光标,并获得最后一张照片的路径是从摄像机捕捉。

 光标光标= getContentResolver()查询(Media.EXTERNAL_CONTENT_URI,新的String [] {Media.DATA,Media.DATE_ADDED,MediaStore.Images.ImageColumns.ORIENTATION},Media.DATE_ADDED ,空,DATE_ADDED ASC);
如果(光标= NULL和放大器;!&安培; cursor.moveToFirst())
{
    做 {
        的uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
        photoPath = uri.toString();
    }而(cursor.moveToNext());
    cursor.close();
}
 

i work with android 2.1 , and i want to get real path from Camera intent result. I read Get Path of image from ACTION_IMAGE_CAPTURE intent but it is for android 2.2.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == CAMERA_RESULT)
    {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        imv.setImageBitmap(thumbnail);
         Uri selectedImageUri = data.getData();
         String path = getRealPathFromURI(selectedImageUri);
    }
}

private String getRealPathFromURI(Uri contentUri)
{
    try
    {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    catch (Exception e)
    {
        return contentUri.getPath();
    }
}

解决方案

Its above code works in some mobile but does not work in samsung mobile in my case so I implemented the common logic for all devices.

When I capture the photo from camera so I implement a logic using Cursor and iterate the cursor and get the last photo path which is capture from camera.

Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if(cursor != null && cursor.moveToFirst())
{
    do {
        uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
        photoPath = uri.toString();
    }while(cursor.moveToNext());
    cursor.close();
}

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

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