Android 微信通过第三方的软件打开,获取文件路径

 微信的返回的fileprovider地址
 "content://com.tencent.mm.external.fileprovider/"
 "content://[email protected]/"
	//通过微信URI解析文件地址
    public static String uriToFile(Context context, Uri uri) {
        try {
            File file = null;
            if (uri == null) return null;
            //获得ContentResolver,用于访问其它应用数据
            ContentResolver resolver = context.getContentResolver();
            //获得URI路径
            String pathUri = uri.getPath().toLowerCase();
            //从路径中提取文件名+文件扩展名
            String displayName = getFileName(pathUri) + "." + getExtensionName(pathUri);
            //解析微信文件路径
            if (uri.toString().toLowerCase().startsWith("content://com.tencent.mm.external.fileprovider/") || uri.toString().toLowerCase().startsWith("content://[email protected]/")) {
                try {
                    InputStream is = resolver.openInputStream(uri);
                    File cache = new File(context.getCacheDir().getAbsolutePath(), displayName);
                    FileOutputStream fos = new FileOutputStream(cache);
                    //android.os.FileUtils不是工具类,是Android的SDK方法
                    android.os.FileUtils.copy(is, fos);
                    file = cache;
                    fos.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return file.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(Android,android,微信)