因?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格式,這里分兩種情況:
如果是媒體庫(kù)查詢的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.reibang.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;
}
終于大功告成!5脖啤括改!
發(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ǔ)充屡久。
完整代碼請(qǐng)看github地址