微信和QQ在其他应用打开列表中添加自己的应用,并获取文件路径

2019-05-06  本文已影响0人  CoolKin

因为项目需求,要将微信和QQ等第三方应用里的文件直接分享到自己的应用。在实际做的过程中,遇到了一些问题,在这里记录一下

因为之前做过图片分享到自己的应用,以为只需要添加下面代码就可以

 <!--调用分享或发送时,应用列表添加自己应用  -->
 <intent-filter>
       <action android:name="android.intent.action.SEND" />
       <category android:name="android.intent.category.DEFAULT" />
       <!-- 允许所有类型文件-->
       <data android:mimeType="*/*" />
       <!-- 只允许图片文件-->
       <!--<data android:mimeType="image/*"/>-->
 </intent-filter>

结果在微信中打开文件,选择发送,显示的列表中有自己的应用,但打开文件时选择“其他应用打开”的列表中还是没有自己的应用,经过一番查找 。。。。 配置中action需要改成view

<!--调用微信的其他应用打开列表添加自己应用  -->
<intent-filter>
       <action android:name="android.intent.action.VIEW" />
       <category android:name="android.intent.category.DEFAULT" />
       <data android:scheme="content"/>
       <!-- 允许所有类型文件-->
       <data android:mimeType="*/*" />
       <!-- 只允许图片文件-->
       <!--<data android:mimeType="image/*"/>-->
</intent-filter>

很兴奋的以为解决了,结果微信可以了,但QQ不行,崩溃了。。。
再经过一阵百度,找到了解决方法,将android:scheme的content改成file就可以了

<!--调用QQ的其他应用打开列表添加自己应用  -->
<intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:scheme="file"/>
      <!-- 允许所有类型文件-->
      <data android:mimeType="*/*" />
      <!-- 只允许图片文件-->
      <!--<data android:mimeType="image/*"/>-->
</intent-filter>

应用列表解决了,结果在获取文件路径的时候又有问题了,晕~~

当action为android.intent.action.SEND时,获取路径方法:

Uri uri = getIntent().getExtras().getParcelable(Intent.EXTRA_STREAM);
String url = uri.toString();
Log.e("url", url);

在自己文件管理选择本应用分享时,打印的url地址为 content://media/external/file/85139
在微信使用QQ浏览器打开,再发送本应用时,打印的url地址为
file:///storage/emulated/0/tencent/MicroMsg/Download/111.doc

当action为android.intent.action.View时,获取路径方法:

Intent intent = getIntent();
Uri  uri = intent.getData();
String  url = intent.getDataString();
Log.e("url", url);

在自己文件管理选择本应用打开时,url的值为content://media/external/file/85139
在微信中选择本应用打开时,url的值为
content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
在QQ中选择本应用打开时,url的值为
file:///storage/emulated/0/tencent/MicroMsg/Download/111.doc

获取本地文件file格式路径可以直接使用,uri需要转成file格式,这里分两种情况:
如果是媒体库查询的uri,如content://media/external/file/85139 格式的转file方法:

public static String getFilePathFromContentUri(Uri selectedVideoUri,
                                                   Activity context) {
        String filePath = "";
        String[] filePathColumn = {MediaStore.MediaColumns.DATA};

//      Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
//      也可用下面的方法拿到cursor
      Cursor cursor = context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            filePath = cursor.getString(columnIndex);
            try {
                //4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
                if (Integer.parseInt(Build.VERSION.SDK) < 14) {
                    cursor.close();
                }
            } catch (Exception e) {
                Log.e("转换地址", "error:" + e);
            }
        }
        return filePath;
    }

如果是fileprovider提供的contenturi,如content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc 要转换成file格式方法(这里参照并借鉴 https://www.jianshu.com/p/0ca6989f2bc2):

public 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;
    }

终于大功告成!!!

上一篇 下一篇

猜你喜欢

热点阅读