检索绝对路径时,从图库奇巧的android选择图片 [英] retrieve absolute path when select image from gallery kitkat android

查看:160
本文介绍了检索绝对路径时,从图库奇巧的android选择图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我支持我的应用程序,以奇巧的版本,现在在这个检索库文件的方式是不同的。

As I am supporting my app to Kitkat version, now in this the way of retrieve file from gallery was different.

我有preferred此的Andr​​oid画廊的奇巧返回不同的URI Intent.ACTION_GET_CONTENT 从库中检索文件并成功地工作,但我需要该文件的绝对路径,我正在

I have preferred this Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT for retrieving file from gallery and successfully work but I required Absolute path of that file, I am getting

content://com.android.providers.media.documents/document/image:2505

有关19以下的版本,我们使用不同的URI通过使用我得到的路径这样

For 19 below version we used uri different by using that I am getting path this way

Cursor cursor = this.getContentResolver().query(originalUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String fpath = cursor.getString(column_index);

但在19版本,它会给我空值怎么弄这是由用户选择图像文件的绝对路径。

but in 19 version it will give me null value how to get absolute path of image file which was selected by user.

感谢

推荐答案

下面是选择文件后访问的绝对路径的一种方式。

Here is one way to access the Absolute path after selecting file.

在获得新的URI格式KK(奇巧)的数据是这样的方式后

After getting data in new URI format for KK(KitKat) like this way

content://com.android.providers.media.documents/document/image:2505

只需提取文档的ID

Just extract ID of your document

if(requestCode == GALLERY_KITKAT_INTENT_CALLED && resultCode == RESULT_OK){

    Uri originalUri = data.getData();

    final int takeFlags = data.getFlags()
                        & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    // Check for the freshest data.
    getContentResolver().takePersistableUriPermission(originalUri, takeFlags);

    /* now extract ID from Uri path using getLastPathSegment() and then split with ":"
    then call get Uri to for Internal storage or External storage for media I have used getUri()
    */

    String id = originalUri.getLastPathSegment().split(":")[1]; 
    final String[] imageColumns = {MediaStore.Images.Media.DATA };
    final String imageOrderBy = null;

    Uri uri = getUri();
    String selectedImagePath = "path";

    Cursor imageCursor = managedQuery(uri, imageColumns,
          MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);

    if (imageCursor.moveToFirst()) {
        selectedImagePath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
    }
    Log.e("path",selectedImagePath ); // use selectedImagePath 
}else if() {
      // for older version use existing code here
}

// By using this method get the Uri of Internal/External Storage for Media
private Uri getUri() {
    String state = Environment.getExternalStorageState();
    if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
        return MediaStore.Images.Media.INTERNAL_CONTENT_URI;

    return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}

这篇关于检索绝对路径时,从图库奇巧的android选择图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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