微信和QQ在其他應(yīng)用打開列表中添加自己的應(yīng)用,并獲取文件路徑

因?yàn)轫?xiàng)目需求,要將微信和QQ等第三方應(yīng)用里的文件直接分享到自己的應(yīng)用。在實(shí)際做的過程中,遇到了一些問題,在這里記錄一下

因?yàn)橹白鲞^圖片分享到自己的應(yīng)用,以為只需要添加下面代碼就可以

 <!--調(diào)用分享或發(fā)送時(shí),應(yīng)用列表添加自己應(yīng)用  -->
 <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>

結(jié)果在微信中打開文件,選擇發(fā)送,顯示的列表中有自己的應(yīng)用,但打開文件時(shí)選擇“其他應(yīng)用打開”的列表中還是沒有自己的應(yīng)用,經(jīng)過一番查找 。。。。 配置中action需要改成view

<!--調(diào)用微信的其他應(yīng)用打開列表添加自己應(yīng)用  -->
<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>

很興奮的以為解決了,結(jié)果微信可以了,但QQ不行,崩潰了。。。
再經(jīng)過一陣百度,找到了解決方法,將android:scheme的content改成file就可以了

<!--調(diào)用QQ的其他應(yīng)用打開列表添加自己應(yīng)用  -->
<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>

應(yīng)用列表解決了,結(jié)果在獲取文件路徑的時(shí)候又有問題了,暈~~

當(dāng)action為android.intent.action.SEND時(shí),獲取路徑方法:

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

在自己文件管理選擇本應(yīng)用分享時(shí),打印的url地址為 content://media/external/file/85139
在微信使用QQ瀏覽器打開,再發(fā)送本應(yīng)用時(shí),打印的url地址為
file:///storage/emulated/0/tencent/MicroMsg/Download/111.doc

當(dāng)action為android.intent.action.View時(shí),獲取路徑方法:

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

在自己文件管理選擇本應(yīng)用打開時(shí),url的值為content://media/external/file/85139
在微信中選擇本應(yīng)用打開時(shí),url的值為
content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
在QQ中選擇本應(yīng)用打開時(shí),url的值為
file:///storage/emulated/0/tencent/MicroMsg/Download/111.doc
好像新版本的url地址也改成用fileprovider的了,如下
content://com.tencent.mobileqq.fileprovider/external_files/storage/emulated/0/Android/data/com.tencent.mobileqq/Tencent/QQfile_recv/1111.doc

獲取本地文件file格式路徑可以直接使用,uri需要轉(zhuǎn)成file格式,這里分兩種情況:
如果是媒體庫查詢的uri,如content://media/external/file/85139 格式的轉(zhuǎn)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以上的版本會(huì)自動(dòng)關(guān)閉 (4.0--14;; 4.0.3--15)
                if (Integer.parseInt(Build.VERSION.SDK) < 14) {
                    cursor.close();
                }
            } catch (Exception e) {
                Log.e("轉(zhuǎn)換地址", "error:" + e);
            }
        }
        return filePath;
    }

如果是fileprovider提供的contenturi,如content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc 要轉(zhuǎn)換成file格式方法(這里參照并借鑒 http://www.itdecent.cn/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;
    }

終于大功告成!??!

發(fā)現(xiàn)好多朋友留言一直失敗,我自己重新試了下,發(fā)現(xiàn)在運(yùn)行g(shù)etFPUriToPath方法時(shí),地址一直轉(zhuǎn)變不成功,后來經(jīng)過研究發(fā)現(xiàn),微信的fileprovider是com.tencent.mm.external.fileprovider,QQ的fileprovider是com.tencent.mobileqq.fileprovider他們兩個(gè)在這句代碼provider.name.equalsIgnoreCase(fileProviderClassName) 獲取provider.name時(shí)是android.support.v4.content.FileProvider,也就是v4包下的,但現(xiàn)在AS版本高了,在建項(xiàng)目時(shí),都是用的androidx包下的,也就是androidx.core.content.FileProvider,所以導(dǎo)致provider名稱一直不一致,因此uri地址一直轉(zhuǎn)變不成url,將FileProvider.class這個(gè)類引入換成v4包下后就成功了。當(dāng)然,可以找找其他uri轉(zhuǎn)url方法,暫時(shí)沒找到,等找到后再補(bǔ)充。

完整代碼請看github地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容