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

发布时间:2020-12-03 14:45:57 作者:Leah
来源:亿速云 阅读:164

本篇文章为大家展示了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 应用中打开,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 使用Python怎么将doc格式转化为pdf格式
  2. 本地pdf文件如何在Android应用中打开

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android pdf doc

上一篇:Android应用如何判断用户是否设置了摄像头权限

下一篇:Android Studio应用中的so库怎么利用JNI生成

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》