GetPathFromUri4kitkatAndroid 4.4 kitkat以上及以下根据uri获取路径的方法

Posted HaiyuKing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GetPathFromUri4kitkatAndroid 4.4 kitkat以上及以下根据uri获取路径的方法相关的知识,希望对你有一定的参考价值。

版权声明:本文为HaiyuKing原创文章,转载请注明出处!

前言

android4.4之前和之后,通过Intent调用文件管理器选择文件,获取的文件uri地址形式是不同的。

Android6.0 content://com.android.providers.media.documents/document/image%3A593410

Android4.2.2 file:///storage/emulated/0/Pictures/Screenshots/Screenshot_2017-04-17-14-39-13.png

所以,当我们需要用到文件的绝对路径地址(/storage/emulated/0/Pictures/Screenshots/Screenshot_20170706-113240.png)的时候,则需要一个通用的方法。

效果图

Android 6.0 Android4.2.2

 

 

 

 

 

代码分析

一般使用getPath( context,uri)方法即可。

使用步骤

一、项目组织结构图

注意事项:

1、导入类文件后需要change包名以及重新import R文件路径

2、Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖

二、导入步骤

将GetPathFromUri4kitkat.java文件复制到项目中

package com.why.project.getpathfromuri4kitkatdemo.utils;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

import java.io.File;

/**
 * @CreateBy HaiyuKing
 * @Used Android 4.4 kitkat以上及以下根据uri获取路径的方法
 * @参考资料 http://www.2cto.com/kf/201502/376975.html
 */
public class GetPathFromUri4kitkat {
    /**
     * 专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使
     * @param uri - Android6.0 content://com.android.providers.media.documents/document/image%3A593410
     *            Android4.2.2 file:///storage/emulated/0/Pictures/Screenshots/Screenshot_2017-04-17-14-39-13.png
     * @return Android6.0 /storage/emulated/0/Pictures/Screenshots/Screenshot_20170706-113240.png
     *          Android4.2.2 /storage/emulated/0/Pictures/Screenshots/Screenshot_2017-04-17-14-39-13.png
     */
    @SuppressLint("NewApi")
    public static String getPath(final Context context, final Uri uri) {
 
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
 
        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
 
                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
 
                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {
 
                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
 
                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
 
                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
 
                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };
 
                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
 
        return null;
    }
 
    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     * 
     * @param context
     *            The context.
     * @param uri
     *            The Uri to query.
     * @param selection
     *            (Optional) Filter used in the query. 构造筛选语句
     * @param selectionArgs
     *            (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public static String getDataColumn(Context context, Uri uri, String selection,
            String[] selectionArgs) {
 
        Cursor cursor = null;

        //筛选列
        final String column = MediaStore.Files.FileColumns.DATA;//"_data"
        final String[] projection = { column };
 
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        }catch (IllegalArgumentException e){
            //java.lang.IllegalArgumentException: column \'_data\' does not exist
            //华为的特殊处理:content://com.huawei.hidisk.fileprovider/root/storage/emulated/0/tencent/TIMfile_recv/xxx.doc
            String rootPre = File.separator + "root";// /root
            return uri.getPath().startsWith(rootPre) ? uri.getPath().replace(rootPre,"") : uri.getPath();
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }
 
    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }
 
    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }
 
    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }
}
GetPathFromUri4kitkat.java

在AndroidManifest.xml中添加权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.why.project.getpathfromuri4kitkatdemo">

    <!-- ======================(GetPathFromUri4kitkat)========================== -->
    <!-- 向SD卡读取数据权限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

 添加运行时权限的处理(本demo中采用的是修改targetSDKVersion=22)

三、使用方法

package com.why.project.getpathfromuri4kitkatdemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.why.project.getpathfromuri4kitkatdemo.utils.GetPathFromUri4kitkat;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Button btn_openFile;
    private TextView tv_filePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initEvents();

    }

    private void initViews() {
        btn_openFile = (Button) findViewById(R.id.btn_openFile);
        tv_filePath = (TextView) findViewById(R.id.tv_filePath);
    }

    private void initEvents() {

        btn_openFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
                String IMAGE_UNSPECIFIED = "*/*";
                innerIntent.setType(IMAGE_UNSPECIFIED); // 查看类型
                Intent wrapperIntent = Intent.createChooser(innerIntent, "File Browser");
                MainActivity.this.startActivityForResult(wrapperIntent, 1111);
            }
        });
    }

    /*=========================================实现打开文件管理器功能==============================================*/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.w(TAG, "{onActivityResult}resultCode=" + resultCode);
        Log.w(TAG, "{onActivityResult}requestCode=" + requestCode);
        if (resultCode == Activity.RESULT_OK) {
            //调用文件管理器选择文件的回调
            if (requestCode == 1111) {
                Uri result = data == null ? null : data.getData();
                Log.w(TAG, "{onActivityResult}result=" + result);

                tv_filePath.setText("Uri:"+result.toString());
                String pathStr = GetPathFromUri4kitkat.getPath(MainActivity.this,result);
                Log.w(TAG, "{onActivityResult}pathStr=" + pathStr);
                tv_filePath.setText(tv_filePath.getText() + "\\n\\n" + "Path:"+pathStr);
            }
        }
    }
}

混淆配置

参考资料

Android 4.4 kitkat以上及以下根据uri获取路径的方法

项目demo下载地址

https://github.com/haiyuKing/GetPathFromUri4kitkatDemo

以上是关于GetPathFromUri4kitkatAndroid 4.4 kitkat以上及以下根据uri获取路径的方法的主要内容,如果未能解决你的问题,请参考以下文章