热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

doc什么格式android能打开软件,PDF、Doc与Dwg格式的文件怎么在Android应用中打开

PDF、Doc与Dwg格式的文件怎么在Android应用中打开发布时间:2020-12-0314:45:57来源:亿速云阅读:95作者&#

PDF、Doc与Dwg格式的文件怎么在Android 应用中打开

发布时间:2020-12-03 14:45:57

来源:亿速云

阅读:95

作者:Leah

本篇文章为大家展示了PDF、Doc与Dwg格式的文件怎么在Android 应用中打开,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

代码:

这是一个单独的类 首先接收intent传过来的url我是用url的后14位作为存储本地的文件名(这里根据自己服务器的文件命名规则而定) 拿到文件路径之后 判断本地是否有此文件 有则打开没有则从服务器上下载并打开 ;

Intent intent = act.getIntent();

final String Strname = intent.getStringExtra("docurl");

//截取最后14位 作为文件名

String s = Strname.substring(Strname.length()-14);

//文件存储

file1 = new File(Environment.getExternalStorageDirectory(), getFileName(s));

new Thread() {

public void run() {

File file = new File( file1.getAbsolutePath());

//判断是否有此文件

if (file.exists()) {

//有缓存文件,拿到路径 直接打开

Message msg = Message.obtain();

msg.obj = haha;

msg.what = DOWNLOAD_SUCCESS;

handler.sendMessage(msg);

mProgressDialog.dismiss();

return;

}

// 本地没有此文件 则从网上下载打开

File downloadfile = downLoad(Strname, file1.getAbsolutePath(), mProgressDialog);

// Log.i("Log",file1.getAbsolutePath());

Message msg = Message.obtain();

if (downloadfile != null) {

// 下载成功,安装....

msg.obj = downloadfile;

msg.what = DOWNLOAD_SUCCESS;

} else {

// 提示用户下载失败.

msg.what = DOWNLOAD_ERROR;

}

handler.sendMessage(msg);

mProgressDialog.dismiss();

};

}.start();

下载文档代码;

传入需要下载的文档的url 和存入内存的路径和dialog

public static File downLoad(String serverpath, String savedfilepath, ProgressDialog pd) {

try {

URL url = new URL(serverpath);

HttpURLConnection cOnn= (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5000);

if (conn.getResponseCode() == 200) {

int max = conn.getContentLength();

pd.setMax(max);

InputStream is = conn.getInputStream();

File file = new File(savedfilepath);

FileOutputStream fos = new FileOutputStream(file);

int len = 0;

byte[] buffer = new byte[1024];

int total = 0;

while ((len = is.read(buffer)) != -1) {

fos.write(buffer, 0, len);

total += len;

pd.setProgress(total);

}

fos.flush();

fos.close();

is.close();

return file;

} else {

return null;

}

} catch (Exception e) {

e.printStackTrace();

}

}

打开文件选择器

Handler handler = new Handler() {

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case DOWNLOAD_SUCCESS:

File file = (File) msg.obj;

Intent intent = new Intent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

intent.setDataAndType (Uri.fromFile(file), "application/pdf");

// startActivity(intent);

startActivity(Intent.createChooser(intent, "标题"));

/**

* 弹出选择框之后 把本activity销毁

*/

finish();

break;

case DOWNLOAD_ERROR:

Util.showToast(act,"文件加载失败");

break;

}

}

};

整体代码

public class list_item_doc extends BaseActivity {

private ProgressDialog mProgressDialog;

// 下载失败

public static final int DOWNLOAD_ERROR = 2;

// 下载成功

public static final int DOWNLOAD_SUCCESS = 1;

private File file1;

@Override

protected void onCreate(Bundle arg0) {

// TODO Auto-generated method stub

super.onCreate(arg0);

initView();

}

private void initView() {

// TODO Auto-generated method stub

Intent intent = act.getIntent();

final String Strname = intent.getStringExtra("url");

mProgressDialog = new ProgressDialog(act);

mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

mProgressDialog.setCancelable(false);

mProgressDialog.show();

//截取最后14位 作为文件名

String s = Strname.substring(Strname.length()-14);

//文件存储

file1 = new File(Environment.getExternalStorageDirectory(), getFileName(s));

new Thread() {

public void run() {

File haha = new File( file1.getAbsolutePath());

//判断是否有此文件

if (haha.exists()) {

//有缓存文件,拿到路径 直接打开

Message msg = Message.obtain();

msg.obj = haha;

msg.what = DOWNLOAD_SUCCESS;

handler.sendMessage(msg);

mProgressDialog.dismiss();

return;

}

// 本地没有此文件 则从网上下载打开

File downloadfile = downLoad(Strname, file1.getAbsolutePath(), mProgressDialog);

// Log.i("Log",file1.getAbsolutePath());

Message msg = Message.obtain();

if (downloadfile != null) {

// 下载成功,安装....

msg.obj = downloadfile;

msg.what = DOWNLOAD_SUCCESS;

} else {

// 提示用户下载失败.

msg.what = DOWNLOAD_ERROR;

}

handler.sendMessage(msg);

mProgressDialog.dismiss();

};

}.start();

}

/**

* 下载完成后 直接打开文件

*/

Handler handler = new Handler() {

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case DOWNLOAD_SUCCESS:

File file = (File) msg.obj;

Intent intent = new Intent("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

intent.setDataAndType (Uri.fromFile(file), "application/pdf");

// startActivity(intent);

startActivity(Intent.createChooser(intent, "标题"));

/**

* 弹出选择框 把本activity销毁

*/

finish();

break;

case DOWNLOAD_ERROR:

Util.showToast(act,"文件加载失败");

break;

}

}

};

/**

*

*/

/**

* 传入文件 url 文件路径 和 弹出的dialog 进行 下载文档

*/

public static File downLoad(String serverpath, String savedfilepath, ProgressDialog pd) {

try {

URL url = new URL(serverpath);

HttpURLConnection cOnn= (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5000);

if (conn.getResponseCode() == 200) {

int max = conn.getContentLength();

pd.setMax(max);

InputStream is = conn.getInputStream();

File file = new File(savedfilepath);

FileOutputStream fos = new FileOutputStream(file);

int len = 0;

byte[] buffer = new byte[1024];

int total = 0;

while ((len = is.read(buffer)) != -1) {

fos.write(buffer, 0, len);

total += len;

pd.setProgress(total);

}

fos.flush();

fos.close();

is.close();

return file;

} else {

return null;

}

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

public static String getFileName(String serverurl) {

return serverurl.substring(serverurl.lastIndexOf("/") + 1);

}

}

上述内容就是PDF、Doc与Dwg格式的文件怎么在Android 应用中打开,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。



推荐阅读
  • FinOps 与 Serverless 的结合:破解云成本难题
    本文探讨了如何通过 FinOps 实践优化 Serverless 应用的成本管理,提出了首个 Serverless 函数总成本估计模型,并分享了多种有效的成本优化策略。 ... [详细]
  • 本文详细介绍如何使用arm-eabi-gdb调试Android平台上的C/C++程序。通过具体步骤和实用技巧,帮助开发者更高效地进行调试工作。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • CentOS系统安装与配置常见问题及解决方案
    本文详细介绍了在CentOS系统安装过程中遇到的常见问题及其解决方案,包括Vi编辑器的操作、图形界面的安装、网络连接故障排除等。通过本文,读者可以更好地理解和解决这些常见问题。 ... [详细]
  • Coursera ML 机器学习
    2019独角兽企业重金招聘Python工程师标准线性回归算法计算过程CostFunction梯度下降算法多变量回归![选择特征](https:static.oschina.n ... [详细]
  • 在Fedora 31上部署PostgreSQL 12
    本文详细介绍如何在Fedora 31操作系统上安装和配置PostgreSQL 12数据库。包括环境准备、安装步骤、配置优化以及安全设置,确保数据库能够稳定运行并提供高效的性能。 ... [详细]
  • Symfony是一个功能强大的PHP框架,以其依赖注入(DI)特性著称。许多流行的PHP框架如Drupal和Laravel的核心组件都基于Symfony构建。本文将详细介绍Symfony的安装方法及其基本使用。 ... [详细]
  • 本文详细介绍了在企业级项目中如何优化 Webpack 配置,特别是在 React 移动端项目中的最佳实践。涵盖资源压缩、代码分割、构建范围缩小、缓存机制以及性能优化等多个方面。 ... [详细]
author-avatar
行者师兄2502861743
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有