美文网首页
如何解析Fileprovider返回的uri

如何解析Fileprovider返回的uri

作者: 2eb56199844d | 来源:发表于2018-05-29 14:40 被阅读434次
  • 4.4之后直接启动系统doumentui,
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.setType("audio/*"); Intent intent1 = Intent.createChooser(intent, "yinyue"); startActivityForResult(intent, 10);
    如果是原生系统,会仅启动系统文件系统,但是国产手机会启动所有的可以打开文件的app,返回的uri会有非常多的可能,
  • 媒体库查询的id
  • document查询的id
  • fileprovider提供的contenturi(最难解析的一中)
        MediaBean bean = null;
        Cursor mCursor = null;
        boolean documentUri = DocumentsContract.isDocumentUri(context, uri);
// 如果是系统document content://com.android.providers.media.documents/document/audio%3A2581
        if (documentUri) {
            Uri mImageUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            final String selection = MediaStore.Audio.Media._ID + "=?";
            final String[] selectionArgs = new String[]{DocumentsContract.getDocumentId(uri).split(":")[1]};
            mCursor = context.getContentResolver().query(mImageUri,
                    proj, selection, selectionArgs, null);
        } else {
            String authority = uri.getAuthority();
// 如果是系统媒体库 content://media/external/audio/media/616028
            if (authority.equalsIgnoreCase("media")) {
                mCursor = context.getContentResolver().query(uri,
                        proj, null, null, null);
            } else {
// 如果是fileprovider. content://com.chaozhuo.filemanager.phoenixos.FileProvider/external/bluetooth/
                final String selection = MediaStore.Audio.Media.DATA + " like ?";
                String path = uri.getPath();
                final int splitIndex = path.indexOf('/', 1);
                path = Uri.decode(path.substring(splitIndex + 1));
                final String[] selectionArgs = new String[]{"%" + path};
                mCursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        proj, selection, selectionArgs, null);
            }
        }

        if (mCursor != null) {
            while (mCursor.moveToNext()) {
                // 获取视频的路径
                int videoId = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media._ID));
                String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String displayName = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                int duration = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                if (duration < 1000) {
//                continue;
                }
                long size = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
                if (size < 0) {
                    //某些设备获取size<0,直接计算
                    Log.e("dml", "this video size < 0 " + path);
                    size = new File(path).length();
                }
                //存储对应关系
                bean = new MediaBean(MediaBean.AUDIO, path, "", displayName, duration, size);
                Log.e("12345", "MediaBean = " + bean);
            }
            mCursor.close();
        }
        return bean;
    }`

- 反射 小米会崩溃
/**
 * 获取FileProvider path
 * author zx
 * version 1.0
 * since 2018/5/4  .
 */
private static String getFPUriToPath(Context context, Uri uri) {
    try {
        List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
        if (packs != null) {
            String fileProviderClassName = FileProvider.class.getName();
            for (PackageInfo pack : packs) {
                ProviderInfo[] providers = pack.providers;
                if (providers != null) {
                    for (ProviderInfo provider : providers) {
                        if (uri.getAuthority().equals(provider.authority)) {
                            if (provider.name.equalsIgnoreCase(fileProviderClassName)) {
                                Class<FileProvider> fileProviderClass = FileProvider.class;
                                try {
                                    Method getPathStrategy = fileProviderClass.getDeclaredMethod("getPathStrategy", Context.class, String.class);
                                    getPathStrategy.setAccessible(true);
                                    Object invoke = getPathStrategy.invoke(null, context, uri.getAuthority());
                                    if (invoke != null) {
                                        String PathStrategyStringClass = FileProvider.class.getName() + "$PathStrategy";
                                        Class<?> PathStrategy = Class.forName(PathStrategyStringClass);
                                        Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Uri.class);
                                        getFileForUri.setAccessible(true);
                                        Object invoke1 = getFileForUri.invoke(invoke, uri);
                                        if (invoke1 instanceof File) {
                                            String filePath = ((File) invoke1).getAbsolutePath();
                                            return filePath;
                                        }
                                    }
                                } catch (NoSuchMethodException e) {
                                    e.printStackTrace();
                                } catch (InvocationTargetException e) {
                                    e.printStackTrace();
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                }
                                break;
                            }
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}```

相关文章

网友评论

      本文标题:如何解析Fileprovider返回的uri

      本文链接:https://www.haomeiwen.com/subject/wcqajftx.html