Android word /excel/ppt 显示

来源:互联网 发布:淘宝返红包怎么用 编辑:程序博客网 时间:2024/05/18 02:35

点击某个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