当前位置: 首页 > 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 "";}}

相关文章:

  • 接口自动化测试-效果展示
  • 2025年文化交流与创新教育国际会议(ICCEIE 2025)
  • 合成来源图以在入侵检测系统中进行数据增强
  • RAG质量评估
  • 【易飞】通过信息传递触发时机复制生成品号实现复制品号自动带出原自定义字段数据
  • 马克思主义与社会科学方法论通俗版
  • MeanFlow:何凯明新作,单步去噪图像生成新SOTA
  • DAY 19 常见的特征筛选算法
  • 本周四19点,《国产网络音频传输的今天和明天》开讲!
  • 软件工程教学评价
  • 性能测试|有限元软件分析——以Abaqus隐式静力学求解为例
  • 【JavaSE】多线程基础学习笔记
  • 网络基础概念(网络基础)
  • Excel表格数据导入数据库
  • 亮相GAITC 2025,中科曙光全面赋能AI基础设施
  • 【为什么RabbitMQ能够控制事务?控制事务的原理】
  • 【第一章:人工智能基础】01.Python基础及常用工具包-(3)常用数据科学工具包
  • find查找指定文件
  • java复习 06
  • shell脚本质数判断
  • 美食网站开发步骤/如何快速收录一个网站的信息
  • 深圳西乡网站建设/seo网站自动推广
  • b站怎么付费推广自己的视频/微营销平台
  • 用html做的游戏网站/建网站软件
  • 安庆网站建设专业制/长沙百度seo代理
  • 邢台企业做网站报价/网站目录提交