android 10.0系统 跳转 使用WPS打开文档

android 10.0系统 跳转 使用WPS打开文档

1、AndroidManifest.xml配置

<provider

android:name="android.support.v4.content.FileProvider"

android:authorities="com.tianci.localmedia.FileProvider"

android:exported="false"

android:grantUriPermissions="true">

<!--指定Uri的共享路径-->

<meta-data

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths" />

</provider>

2、res/xml/file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>

<paths>

<external-path

name="external_storage_root"

path="." />

<files-path

name="files-path"

path="." />

<cache-path

name="cache-path"

path="." />

<!--/storage/emulated/0/Android/data/...-->

<external-files-path

name="external_file_path"

path="." />

<!--代表app 外部存储区域根目录下的文件 Context.getExternalCacheDir目录下的目录-->

<external-cache-path

name="external_cache_path"

path="." />

<!--配置root-path。这样子可以读取到sd卡和一些应用分身的目录,否则微信分身保存的图片,就会导致 java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/999/tencent/MicroMsg/WeiXin/export1544062754693.jpg,在小米6的手机上微信分身有这个crash,华为没有

-->

<root-path

name="root"

path="." />

</paths>

使用:

try{

File file = new File(path);

Bundle bundle = new Bundle();

bundle.putString(WpsModel.OPEN_MODE, WpsModel.OpenMode.NORMAL); // 打开模式

bundle.putBoolean(WpsModel.ENTER_REVISE_MODE, true); // 以修订模式打开文档

bundle.putBoolean(WpsModel.SEND_CLOSE_BROAD, true); // 文件关闭时是否发送广播

bundle.putBoolean(WpsModel.SEND_SAVE_BROAD, true); // 文件保存时是否发送广播

bundle.putBoolean(WpsModel.HOMEKEY_DOWN, true); // 单机home键是否发送广播

bundle.putBoolean(WpsModel.BACKKEY_DOWN, true); // 单机back键是否发送广播

bundle.putBoolean(WpsModel.SAVE_PATH, true); // 文件这次保存的路径

bundle.putString(WpsModel.THIRD_PACKAGE, WpsModel.PackageName.NORMAL); // 第三方应用的包名,用于对改应用合法性的验证

FileOpen.openFile(MainActivity.this,file, bundle);

}catch (Exception e)

{

e.printStackTrace();

}

public class FileOpen {

public static void openFile(Context context, File file, Bundle bundle1){

Bundle bundle = new Bundle();

bundle.putString(WpsModel.OPEN_MODE, WpsModel.OpenMode.NORMAL); // 打开模式

bundle.putBoolean(WpsModel.ENTER_REVISE_MODE, true); // 以修订模式打开文档

bundle.putBoolean(WpsModel.SEND_CLOSE_BROAD, true); // 文件关闭时是否发送广播

bundle.putBoolean(WpsModel.SEND_SAVE_BROAD, true); // 文件保存时是否发送广播

bundle.putBoolean(WpsModel.HOMEKEY_DOWN, true); // 单机home键是否发送广播

bundle.putBoolean(WpsModel.BACKKEY_DOWN, true); // 单机back键是否发送广播

bundle.putBoolean(WpsModel.SAVE_PATH, true); // 文件这次保存的路径

bundle.putString(WpsModel.THIRD_PACKAGE, WpsModel.PackageName.NORMAL); // 第三方应用的包名,用于对改应用合法性的验证

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

//intent.addCategory(Intent.CATEGORY_DEFAULT);

intent.setClassName(WpsModel.PackageName.NORMAL, WpsModel.ClassName.NORMAL);

intent.putExtras(bundle);

if (Build.VERSION.SDK_INT > 23) {//android 7.0以上时,URI不能直接暴露

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Uri uriForFile = FileProvider.getUriForFile(context, "com.tianci.localmedia.FileProvider", file);

intent.setDataAndType(uriForFile, "application/vnd.android.package-archive");

} else {

Uri uri = Uri.fromFile(file);

intent.setDataAndType(uri, "application/vnd.android.package-archive");

}

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent);

}

/**

* 使用自定义方法获得文件的MIME类型

*/

private static String getMimeTypeFromFile(File file) {

String type = "*/*";

String fName = file.getName();

//获取后缀名前的分隔符"."在fName中的位置。

int dotIndex = fName.lastIndexOf(".");

if (dotIndex > 0) {

//获取文件的后缀名

String end = fName.substring(dotIndex, fName.length()).toLowerCase(Locale.getDefault());

//在MIME和文件类型的匹配表中找到对应的MIME类型。

HashMap<String, String> map = MyMimeMap.getMimeMap();

if (!TextUtils.isEmpty(end) && map.keySet().contains(end)) {

type = map.get(end);

}

}

return type;

}

}

猜你喜欢

转载自blog.csdn.net/heshuangqiang/article/details/126634173