本來使用了TBS 但是用戶手機(jī)太雜,經(jīng)常白屏,又加了這個功能。
適配安卓X,話不多說,直接上代碼
首先需要在清單文件中加provider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.nanrui.hlj.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
屬性什么意思大家自己查查 我就不寫了
重點(diǎn)是這個 authorities 為了與其他區(qū)別 都是自己包名加.fileProvider
在那路徑的時候需要使用。
然后其中的provider_paths 自己在res下 創(chuàng)建一個xml文件 新建這個文件
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="Pictures"/>
<!-- /storage/emulated/0/Download/${applicationId}/.beta/apk-->
<external-path name="beta_external_path" path="Download/"/>
<!--/storage/emulated/0/Android/data/${applicationId}/files/apk/-->
<external-path name="beta_external_files_path" path="Android/data/"/>
<cache-path name="cache_path" path="."/>
</paths>
不同的-path 代表不同的路徑 我這里就寫了2種
重點(diǎn)是標(biāo)簽名,name 是構(gòu)建新的路徑用的, path也是,跟原來的沒有影響,都是新的路徑。
接下來是打開wps的方法
public boolean openFile(String path) {
Intent intent = new Intent();
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); // 文件關(guān)閉時是否發(fā)送廣播
bundle.putBoolean(WpsModel.SEND_SAVE_BROAD, true); // 文件保存時是否發(fā)送廣播
bundle.putBoolean(WpsModel.HOMEKEY_DOWN, true); // 單機(jī)home鍵是否發(fā)送廣播
bundle.putBoolean(WpsModel.BACKKEY_DOWN, true); // 單機(jī)back鍵是否發(fā)送廣播
bundle.putBoolean(WpsModel.SAVE_PATH, true); // 文件這次保存的路徑
bundle.putString(WpsModel.THIRD_PACKAGE, WpsModel.PackageName.NORMAL); // 第三方應(yīng)用的包名,用于對改應(yīng)用合法性的驗(yàn)證
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 == null || !file.exists()) {
System.out.println("文件為空或者不存在");
return false;
}
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(getContext(), getPackageName() + ".fileProvider", file);
} else {
uri = Uri.fromFile(file);
}
// intent.setData(uri);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);//給目標(biāo)文件臨時授權(quán)
// intent.setDataAndType(uri,"application/vnd.ms-powerpoint");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//動態(tài)的grant URI權(quán)限給Action接收者
intent.setData(uri);
intent.putExtras(bundle);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
System.out.println("打開wps異常:" + e.toString());
e.printStackTrace();
return false;
}
return true;
}
這里重點(diǎn)是
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
一定要加,不然會拿不到數(shù)據(jù)
最后是wps的配置
public class WpsModel {
public static final String OPEN_MODE = "OpenMode";// 打開文件的模式。
public static final String SEND_SAVE_BROAD = "SendSaveBroad";// 文件保存時是否發(fā)送廣播。
public static final String SEND_CLOSE_BROAD = "SendCloseBroad";// 文件關(guān)閉時是否發(fā)送廣播
public static final String THIRD_PACKAGE = "ThirdPackage";// 第三方的包名,關(guān)閉的廣播會包含該項(xiàng)。
public static final String CLEAR_BUFFER = "ClearBuffer";// 關(guān)閉文件時是否請空臨時文件。
public static final String CLEAR_TRACE = "ClearTrace";// 關(guān)閉文件時是否刪除使用記錄。
public static final String CLEAR_FILE = "ClearFile";// 關(guān)閉文件時是否刪除打開的文件。
public static final String VIEW_PROGRESS = "ViewProgress";// 文件上次查看的進(jìn)度。
public static final String AUTO_JUMP = "AutoJump";// 是否自動跳轉(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)聽home鍵并發(fā)廣播
public static final String BACKKEY_DOWN = "BackKeyDown";// 監(jiān)聽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";// 打開直接進(jìn)入閱讀器模式
// 僅Word、TXT文檔支持
public static final String SAVE_ONLY = "SaveOnly";// 保存模式(打開文件,另存,關(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)閉文件廣播
}
最后 本人也是在網(wǎng)上找的,如果有不對的地方 歡迎大家指正。有用的話點(diǎn)個贊吧。多謝。