離線預(yù)覽服務(wù)需要時(shí)用騰訊x5的文件預(yù)覽服務(wù),支持的類型也有下面這幾種類型:
doc爽彤、docx养盗、ppt、pptx适篙、xls往核、xlsx、pdf嚷节、txt聂儒、epub
官方接入和說明
核心代碼就是下方的這些虎锚,不過效果是會(huì)打開QQ瀏覽器的文件預(yù)覽服務(wù),沒有QQ瀏覽器可能就會(huì)調(diào)用第三方應(yīng)用來打開
HashMap<String, String> params = new HashMap<String, String>();
params.put("style", "1");
params.put("local", "true");
//params.put("memuData", jsondata);
QbSdk.openFileReader(mActivity, filePath, params, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
}
});
如果上述滿足要求衩婚,直接使用即可窜护。
后來通過搜索發(fā)現(xiàn),sdk內(nèi)部有個(gè)TbsReaderView類是可以用來在應(yīng)用內(nèi)部實(shí)現(xiàn)文件預(yù)覽的非春,在布局中添加上這個(gè)View:
mTbsReaderView = new TbsReaderView(this, new TbsReaderView.ReaderCallback() {
@Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
}
});
layout.addView(mTbsReaderView, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
然后在代碼中調(diào)用打開文件就可以了
String bsReaderTemp = FileUtils.getCachePath(this) + "/TbsReaderTemp";//緩存文件夾路徑柄慰,自定義
File bsReaderTempFile = new File(bsReaderTemp);
if (!bsReaderTempFile.exists()) {
boolean mkdir = bsReaderTempFile.mkdir();
if (!mkdir) {
Log.d("print", "創(chuàng)建/TbsReaderTemp失敗K澳取WΑ!>淳亍概行!");
Toast.makeText(this, "打開失敗弧岳!", Toast.LENGTH_SHORT).show();
}
}
Bundle bundle = new Bundle();
bundle.putString("filePath", filePath);
bundle.putString("tempPath", tbsReaderTemp);
boolean result = mTbsReaderView.preOpen(fileType, false);
Log.d("print", "查看文檔---" + result);
if (result) {
mTbsReaderView.openFile(bundle);
} else {
Intent intent = FileUtils.getViewIntent(this, new File(filePath));
//跳轉(zhuǎn)
startActivity(intent);
finish();
}
有些機(jī)型加載插件會(huì)失敗凳忙,目前騰訊官方也不太支持這個(gè)功能,所以在最下方做了容錯(cuò)處理禽炬,有些無法加載的機(jī)型跳轉(zhuǎn)到第三方應(yīng)用去預(yù)覽涧卵,result的值為false是加載不出來的,會(huì)顯示空白腹尖。
下邊是跳轉(zhuǎn)三方應(yīng)用的代碼:
public static Intent getViewIntent(Context context, File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//判斷版本是否在7.0以上
uri = FileProvider.getUriForFile(context,
context.getPackageName() + ".provider",
file);
//添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
}
String url = file.toString().toLowerCase();
if (url.contains(".doc") || url.contains(".docx")) {
// Word document
intent.setDataAndType(uri, "application/msword");
} else if (url.contains(".pdf")) {
// PDF file
intent.setDataAndType(uri, "application/pdf");
} else if (url.contains(".ppt") || url.contains(".pptx")) {
// Powerpoint file
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if (url.contains(".xls") || url.contains(".xlsx")) {
// Excel file
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if (url.contains(".txt")) {
// Text file
intent.setDataAndType(uri, "text/plain");
} else {
intent.setDataAndType(uri, "*/*");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
return intent;
}
這樣就能在應(yīng)用中添加離線文件預(yù)覽服務(wù)了柳恐,不過目前的支持類型較少