參數(shù)輔助
首先是網(wǎng)上找到一個(gè)參數(shù)輔助類,具體不知道出自哪里跨释,我在官方也沒(méi)有找到相關(guān)資料雕旨,輔助使用即可
public class WpsModel {
public static final String OPEN_MODE = "OpenMode";// 打開(kāi)文件的格式
public static final String SEND_SAVE_BROAD = "SendSaveBroad";// 文件保存時(shí)是否發(fā)送廣播
public static final String SEND_CLOSE_BROAD = "SendCloseBroad";// 文件關(guān)閉時(shí)是否發(fā)送廣播
public static final String THIRD_PACKAGE = "ThirdPackage";// 第三方的包名悯姊,關(guān)閉的廣播會(huì)包含該項(xiàng)
public static final String CLEAR_BUFFER = "ClearBuffer";// 關(guān)閉文件時(shí)是否請(qǐng)空臨時(shí)文件
public static final String CLEAR_TRACE = "ClearTrace";// 關(guān)閉文件時(shí)是否刪除使用記錄
public static final String CLEAR_FILE = "ClearFile";// 關(guān)閉文件時(shí)是否刪除打的文件
public static final String VIEW_PROGRESS = "ViewProgress";// 文件上次查看的進(jìn)度
public static final String AUTO_JUMP = "AutoJump";// 是否自動(dòng)跳轉(zhuǎn)到上次查看的進(jìn)度
public static final String SAVE_PATH = "SavePath";// 文件保存路徑
public static final String VIEW_SCALE = "ViewScale";// 文件上次查看的視圖的縮放
public static final String VIEW_SCALE_X = "ViewScrollX";// 文件上次查看的視圖的X坐標(biāo)
public static final String VIEW_SCALE_Y = "ViewScrollY";// 文件上次查看的視圖的Y坐標(biāo)
public static final String USER_NAME = "UserName";// 批注的作者
public static final String HOMEKEY_DOWN = "HomeKeyDown";// 監(jiān)聽(tīng)home鍵并發(fā)廣
public static final String BACKKEY_DOWN = "BackKeyDown";// 監(jiān)聽(tīng)back鍵并發(fā)廣
public static final String ENTER_REVISE_MODE = "EnterReviseMode";// 以修訂模式打文檔
public static final String CACHE_FILE_INVISIBLE = "CacheFileInvisible";// Wps生成的緩存文件外部是否可
public class OpenMode {
public static final String NORMAL = "Normal";// 正常模式
public static final String READ_ONLY = "ReadOnly";// 只讀模式
public static final String READ_MODE = "ReadMode";// 打開(kāi)直接進(jìn)入閱讀器模
public static final String SAVE_ONLY = "SaveOnly";// 保存模式(打開(kāi)文件,另存,關(guān)閉)僅Word、TXT文檔支持
}
public class ClassName {
public static final String NORMAL = "cn.wps.moffice.documentmanager.PreStartActivity2";// 普通版
public static final String ENGLISH = "cn.wps.moffice.documentmanager.PreStartActivity2";// 英文
public static final String ENTERPRISE = "cn.wps.moffice.documentmanager.PreStartActivity2";// 企業(yè)
}
public class PackageName {
public static final String NORMAL = "cn.wps.moffice_eng";// 普通版
public static final String ENGLISH = "cn.wps.moffice_eng";// 英文
}
public class Reciver {
public static final String ACTION_BACK = "com.kingsoft.writer.back.key.down";// 返回鍵廣
public static final String ACTION_HOME = "com.kingsoft.writer.home.key.down";// Home鍵廣
public static final String ACTION_SAVE = "cn.wps.moffice.file.save";// 保存廣播
public static final String ACTION_CLOSE = "cn.wps.moffice.file.close";// 關(guān)閉文件廣播
}
}
FileProvider配置
由于Android7.0將 ==file://URI== 取消掉并改為 ==content:// URI== 池颈,并且還增加了一些權(quán)限的問(wèn)題,所以直接使用文件路徑是不可行的钓丰,這里就需要借助FileProvider躯砰。
首先在AndroidManife.xml中的Application標(biāo)簽下聲明一個(gè)FileProvider,這里provider的name屬性在最新的AndroidX路徑如下携丁。如果不是使用的AndroidX使用的是V4包琢歇,那么路徑應(yīng)該是"android.support.v4.content.FileProvider"。
authorities屬性是自己項(xiàng)目的報(bào)名.fileprovider则北。
meta標(biāo)簽里的resources屬性是xml配置文件的路徑矿微。
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.mustang4w.readusbinfo.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
FileProvider需要的xml配置文件。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="download"
path="" />
</paths>
這里還有一些不同的寫(xiě)法尚揣,根據(jù)自己的需要選取
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="" />
<files-path name="files" path="" />
<cache-path name="cache" path="" />
<external-path name="external" path="" />
<external-files-path name="name" path="path" />
<external-cache-path name="name" path="path" />
</paths>
<!-- <root-path/> 代表設(shè)備的根目錄new File("/"); -->
<!-- <files-path/> 代表context.getFilesDir() -->
<!-- <cache-path/> 代表context.getCacheDir() -->
<!-- <external-path/> 代表Environment.getExternalStorageDirectory() -->
<!-- <external-files-path>代表context.getExternalFilesDirs() -->
<!-- <external-cache-path>代表getExternalCacheDirs() -->
調(diào)用wps打開(kāi)文件
這里通過(guò)FileProvider.getUriForFile()涌矢,將 ==file://URI== 轉(zhuǎn)化為 ==content:// URI== ,然后直接通過(guò)WPS預(yù)留的接口快骗,用Intent直接跳轉(zhuǎn)娜庇。
public static boolean openFile(String path, Context context) {
try {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString(WpsModel.OPEN_MODE, WpsModel.OpenMode.READ_ONLY); // 打開(kāi)模式--只讀模式
bundle.putBoolean(WpsModel.SEND_CLOSE_BROAD, true); // 關(guān)閉時(shí)是否發(fā)送廣播
bundle.putString(WpsModel.THIRD_PACKAGE, context.getPackageName()); // 第三方應(yīng)用的包名,用于對(duì)改應(yīng)用合法性的驗(yàn)證
bundle.putBoolean(WpsModel.CLEAR_TRACE, false);// 清除打開(kāi)記錄
bundle.putInt(WpsModel.VIEW_SCALE_X, 30);
bundle.putInt(WpsModel.VIEW_SCALE_Y, 37);
// bundle.putBoolean(WpsModel.CLEAR_FILE, true); // 關(guān)閉后刪除打開(kāi)文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setClassName(WpsModel.PackageName.NORMAL, WpsModel.ClassName.NORMAL);
File file = new File(path);
if (!file.exists()) {
System.out.println("文件為空或者不存在");
return false;
}
Uri photoURI = FileProvider.getUriForFile(context, "com.mustang4w.readusbinfo.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//授予臨時(shí)權(quán)限別忘了
intent.setData(photoURI);
intent.putExtras(bundle);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
System.out.println("打開(kāi)wps異常:" + e.toString());
e.printStackTrace();
return false;
}
return true;
}