Android离线文件预览服务

离线预览服务需要时用腾讯x5的文件预览服务,支持的类型也有下面这几种类型:
doc、docx、ppt、pptx、xls、xlsx、pdf、txt、epub
官方接入和说明
核心代码就是下方的这些,不过效果是会打开QQ浏览器的文件预览服务,没有QQ浏览器可能就会调用第三方应用来打开

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) {
  }
});

如果上述满足要求,直接使用即可。
后来通过搜索发现,sdk内部有个TbsReaderView类是可以用来在应用内部实现文件预览的,在布局中添加上这个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));

然后在代码中调用打开文件就可以了

String bsReaderTemp = FileUtils.getCachePath(this) + "/TbsReaderTemp";//缓存文件夹路径,自定义
File bsReaderTempFile = new File(bsReaderTemp);

if (!bsReaderTempFile.exists()) {
    boolean mkdir = bsReaderTempFile.mkdir();
    if (!mkdir) {
        Log.d("print", "创建/TbsReaderTemp失败!!!!!");
        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));
    //跳转
    startActivity(intent);
    finish();
}

有些机型加载插件会失败,目前腾讯官方也不太支持这个功能,所以在最下方做了容错处理,有些无法加载的机型跳转到第三方应用去预览,result的值为false是加载不出来的,会显示空白。
下边是跳转三方应用的代码:

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);
        //添加这一句表示对目标应用临时授权该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;
}

这样就能在应用中添加离线文件预览服务了,不过目前的支持类型较少

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容