当前位置: 首页 > news >正文

Android实践:查看远程文档

需求:APP应用中网页打开远程文档,如.pdf,.doc,.xlsx等

方案:

1.下载文件到本地

2.使用三方app打开文件

实现:

1.下载

compile 'com.loopj.android:android-async-http:1.4.11'
public void download(Context context,String url,String dirPath,String fileName){AsyncHttpClient client =new AsyncHttpClient();AsyncDownload asyncDownload = new AsyncDownload();asyncDownload.setAsyncHttpClient(client);asyncDownload.setFileName(dirPath + fileName);File file = new File(dirPath, fileName);
RangeFileAsyncHttpResponseHandler fileAsyncHttpResponseHandler = new RangeFileAsyncHttpResponseHandler(file) {@Overridepublic void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, File file) {}@Overridepublic void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, File file) {}@Overridepublic void onCancel() {super.onCancel();}@Overridepublic void onProgress(long bytesWritten, long totalSize) {super.onProgress(bytesWritten, totalSize);}@Overridepublic void onStart() {super.onStart();}};fileAsyncHttpResponseHandler.setUseSynchronousMode(false);client.setEnableRedirects(true);//同意反复下载client.get(context, url, fileAsyncHttpResponseHandler);
}

 2.打开

public void open(Context context, String docUrl){try {Uri uri = FileProvider.getUriForFile(context, context.getPackageName() +         ".fileProvider", new File(docUrl));Intent intent = new Intent(Intent.ACTION_VIEW);String mimeType = getMimeTypeFromExtension(Uri.parse(docUrl).getLastPathSegment());if (TextUtils.isEmpty(mimeType)) {intent.setData(uri);} else {intent.setDataAndType(uri, mimeType);}intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);context.startActivity(Intent.createChooser(intent,"请选择应用打开"));} catch (Exception e) {e.printStackTrace();}
}public String getMimeTypeFromExtension(String fileName) {String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();switch (extension) {case "doc":return "application/msword";case "docx":return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";case "xls":return "application/vnd.ms-excel";case "xlsx":return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";case "ppt":case "pps":return "application/vnd.ms-powerpoint";case "pptx":return "application/vnd.openxmlformats-officedocument.presentationml.presentation";case "wps":return "application/vnd.ms-works";case "msg":return "application/vnd.ms-outlook";case "mpc":return "application/vnd.mpohun.certificate";case "jar":return "application/java-archive";case "tar":return "application/x-tar";case "tgz":return "application/x-compressed";case "zip":return "application/x-zip-compressed";case "txt":return "text/plain";case "jpg":case "jpeg":return "image/jpeg";case "png":return "image/png";case "gif":return "image/gif";case "mp3":return "audio/*";case "mp4":return "video/mp4";case "rmvb":return "audio/x-pn-realaudio";case "wav":case "wma":case "wmv":return "audio/*";default:return "";}}

http://www.xdnf.cn/news/956899.html

相关文章:

  • 数据驱动证券业务精细化决策,从洞察到行动的全链路赋能
  • IBMS集成系统 结合数字孪生技术,实现建筑的3D可视化监控与智能化运维
  • prxomox 8.3-8.4更新
  • 如何在Android Studio中导出apk文件
  • [25-cv-06277]Keith律所代理昆虫画作版权图
  • docker相关(AI回答)
  • 详解快排的四种方式
  • RT_Thread——线程管理(上)
  • 【系统架构设计师-2025上半年真题】案例分析-参考答案及部分详解(回忆版)
  • 【最新案例】智能物料称重柜/生鲜称重售卖柜系统, 共享自助管理系统, 物联网应用定制开发
  • 如何删除linux空的文件夹
  • 02__C++的基本语法
  • Unity中的Mathf.Lerp
  • ArcGIS Pro+ArcGIS给你的地图加上北回归线!
  • 安宝特案例丨Vuzix AR智能眼镜集成专业软件,助力卢森堡医院药房转型,赢得辉瑞创新奖
  • 什么是EULA和DPA
  • Android Test4 Application(Context)实例的获取
  • 深入探讨渗透测试的定义、关键步骤以及实施方法
  • 《射频识别(RFID)原理与应用》期末复习 RFID第三章 编码和调制(知识点总结+习题巩固)
  • EZDML:一款国产免费的数据库建模工具
  • [25-cv-61102]SMG律所代理Fanatics 体育用品商标维权案
  • UI学习(三)
  • 嵌入式学习Day34
  • VS Code中Augment AI免费额度用尽的完美解决方案
  • CppCon 2015 学习:The Birth of Study Group 14
  • Android15默认开启无障碍服务
  • ui框架-文件上传组件
  • 高密度ARM服务器的散热设计
  • JAVA开发工具箱
  • Easy Excel