Android word /excel/ppt 显示

点击某个ListView中的某个Item,就从网上下载并打开某个word/excel/ppt


这里写图片描述


说明:url为资源的网络地址


public class xianshi extends BaseActivity{

private ProgressDialog mProgressDialog;

    // 下载成功
    public static final int DOWNLOAD_ERROR = 7;
    // 下载失败
    public static final int DOWNLOAD_SUCCESS = 6;
    @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("docurl");
         mProgressDialog = new ProgressDialog(act);
           mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
           mProgressDialog.show();             

           final File file1 = new File( Environment.getExternalStorageDirectory(), getFileName("office" + ".doc"));

               new Thread() {                          
               public void run() {
                   File downloadfile = downLoad(Strname,file1.getAbsolutePath(),mProgressDialog);
                   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();
                    intent.setAction("android.intent.action.VIEW");
                    intent.addCategory("android.intent.category.DEFAULT");
                    intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
                    startActivity(intent);
                    break;
                case DOWNLOAD_ERROR:
                    break;

                default:
                    break;
            }
        }
    };
    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);
                        if (file.exists()) {
                            return file;
                        }
                        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);
            }               
}


FR:海涛高软(QQ技术群:386476712)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值