因为项目需求,要将微信和QQ等第三方应用里的文件直接分享到自己的应用。
1.首先在AndroidManifest.xml中找到其他应用唤起自己应用中的分享Activity
<activity
android:name=".ui.share.ShareActivity"
android:label="@string/share_label"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
<!--调用分享或发送时,应用列表添加自己应用 -->
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content"/>
<!-- 允许所有类型文件-->
<data android:mimeType="*/*" />
<!-- 只允许图片文件-->
</intent-filter>
</activity>
注意这里的action并不是
<action android:name="android.intent.action.SEND" />
而是
<action android:name="android.intent.action.VIEW" />
在ShareActivity中
if (Intent.ACTION_VIEW.equals(mAction)) {
Intent intent=getIntent();
Uri uri=intent.getData();
String url=intent.getDataString();
}
在自己文件管理选择本应用打开时,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格式方法
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;
}
------------------------------------------------------更新于12.18---------------------------------------------------
测试发现出了问题,在其他app手动升级安装的时候,点击安装跳转到了自己应用分享的界面
因为 系统安装的action也是android.intent.action.VIEW
mimeType = "application/vnd.android.package-archive"
写了一个苟且的解决方案,只保留了几个常见的文件类型,因为目前还不知道怎么只过滤"application/vnd.android.package-archive"这一个mimeType
<!--调用分享或发送时,应用列表添加自己应用 -->
<intent-filter tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/msword" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
<data android:mimeType="text/*" />
<data android:mimeType="application/pdf" />
<data android:mimeType="application/vnd.ms-excel" />
<data android:mimeType="application/vnd.ms-powerpoint" />
<data android:mimeType="application/x-zip-compressed" />
</intent-filter>
各种mimeType
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop", "text/plain"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"},
{"", "*/*"}