我是靠谱客的博主 微笑冥王星,最近开发中收集的这篇文章主要介绍从Uri获取文件真实路径,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

public static String getRealPathFromURI(Context var0, Uri var1) {
        boolean var2 = Build.VERSION.SDK_INT >= 19;
        String var3;
        if (var2 && DocumentsContract.isDocumentUri(var0, var1)) {
            String[] var4;
            String var5;
            if (isExternalStorageDocument(var1)) {
                var3 = DocumentsContract.getDocumentId(var1);
                var4 = var3.split(":");
                var5 = var4[0];
                if ("primary".equalsIgnoreCase(var5)) {
                    return Environment.getExternalStorageDirectory() + "/" + var4[1];
                }
            } else {
                if (isDownloadsDocument(var1)) {
                    var3 = DocumentsContract.getDocumentId(var1);
                    Uri var9 = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(var3));
                    return getDataColumn(var0, var9, (String) null, (String[]) null);
                }

                if (isMediaDocument(var1)) {
                    var3 = DocumentsContract.getDocumentId(var1);
                    var4 = var3.split(":");
                    var5 = var4[0];
                    Uri var6 = null;
                    if ("image".equals(var5)) {
                        var6 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    } else if ("VIDEO".equals(var5)) {
                        var6 = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    } else if ("AUDIO".equals(var5)) {
                        var6 = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    }

                    String var7 = "_id=?";
                    String[] var8 = new String[]{var4[1]};
                    return getDataColumn(var0, var6, "_id=?", var8);
                }
            }
        } else {
            var3 = getFPUriToPath(var0, var1);
            if (!TextUtils.isEmpty(var3)) {
                return var3;
            }

            if ("content".equalsIgnoreCase(var1.getScheme())) {
                return getDataColumn(var0, var1, (String) null, (String[]) null);
            }

            if ("file".equalsIgnoreCase(var1.getScheme())) {
                return var1.getPath();
            }
        }

        return null;
    }

    public static boolean isExternalStorageDocument(Uri var0) {
        return "com.android.externalstorage.documents".equals(var0.getAuthority());
    }

    public static boolean isDownloadsDocument(Uri var0) {
        return "com.android.providers.downloads.documents".equals(var0.getAuthority());
    }

    public static boolean isMediaDocument(Uri var0) {
        return "com.android.providers.media.documents".equals(var0.getAuthority());
    }


    public static String getDataColumn(Context var0, Uri var1, String var2, String[] var3) {
        Cursor var4 = null;
        String var5 = "_data";
        String[] var6 = new String[]{"_data"};

        String var8;
        try {
            var4 = var0.getContentResolver().query(var1, var6, var2, var3, (String) null);
            if (var4 == null || !var4.moveToFirst()) {
                return null;
            }

            int var7 = var4.getColumnIndexOrThrow("_data");
            var8 = var4.getString(var7);
        } finally {
            if (var4 != null) {
                var4.close();
            }

        }

        return var8;
    }

    public static String getFPUriToPath(Context var0, Uri var1) {
        try {
            List var2 = var0.getPackageManager().getInstalledPackages(8);
            if (var2 != null) {
                String var3 = FileProvider.class.getName();
                Iterator var4 = var2.iterator();

                while (true) {
                    while (true) {
                        ProviderInfo[] var6;
                        do {
                            if (!var4.hasNext()) {
                                return null;
                            }

                            PackageInfo var5 = (PackageInfo) var4.next();
                            var6 = var5.providers;
                        } while (var6 == null);

                        ProviderInfo[] var7 = var6;
                        int var8 = var6.length;

                        for (int var9 = 0; var9 < var8; ++var9) {
                            ProviderInfo var10 = var7[var9];
                            if (var1.getAuthority().equals(var10.authority)) {
                                if (var10.name.equalsIgnoreCase(var3)) {
                                    Class var11 = FileProvider.class;

                                    try {
                                        Method var12 = var11.getDeclaredMethod("getPathStrategy", Context.class, String.class);
                                        var12.setAccessible(true);
                                        Object var13 = var12.invoke((Object) null, var0, var1.getAuthority());
                                        if (var13 != null) {
                                            String var14 = FileProvider.class.getName() + "$PathStrategy";
                                            Class var15 = Class.forName(var14);
                                            Method var16 = var15.getDeclaredMethod("getFileForUri", Uri.class);
                                            var16.setAccessible(true);
                                            Object var17 = var16.invoke(var13, var1);
                                            if (var17 instanceof File) {
                                                String var18 = ((File) var17).getAbsolutePath();
                                                return var18;
                                            }
                                        }
                                    } catch (NoSuchMethodException var19) {
                                        var19.printStackTrace();
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        } catch (Exception var23) {
            var23.printStackTrace();
        }

        return null;
    }

 

最后

以上就是微笑冥王星为你收集整理的从Uri获取文件真实路径的全部内容,希望文章能够帮你解决从Uri获取文件真实路径所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部