(代码)Android 实现调用wps打开word文档,打开pdf文档

废话不多说,直接看代码,新建个activity,声明,点击事件,省略。亲测可用,看代码 

Button lookdoc;
lookdoc = (Button)findViewById(R.id.lookdoc);
lookdoc.setOnClickListener(looklisten);

OnClickListener looklisten = new OnClickListener() {

		@Override
		public void onClick(View v) {
			DownLoadUtil d = new DownLoadUtil();
			//获取文件的类型
			String filetype = filename.substring(filename.lastIndexOf(".")+1,filename.length());
			if("jpg".equals(filetype)||"jpeg".equals(filetype)||"bmp".equals(filetype)||"gif".equals(filetype)||"png".equals(filetype)){
				Toast.makeText(PreceptInfoActivity.this, "文件类型错误", Toast.LENGTH_LONG).show();
			}else{
				//检测是否安装了wps软件,没有安装则去下载
				if(isAvilible(PreceptInfoActivity.this, "cn.wps.moffice_eng")){
					//先查看本地是否存在此文件。存在就立即访问。否则再去下载
					// 获得存储卡的路径
					String sdpath = Environment.getExternalStorageDirectory()
							+ "/";
					String sdcard2 = sdpath + "download/"+DownLoadUtil.urldecodeUTF8(filename).substring(DownLoadUtil.urldecodeUTF8(filename).lastIndexOf("/")+1);
					File file = new File(sdcard2);
					// 判断文件目录是否存在
					if (file.exists()) {
						Intent intent = new Intent();
						Bundle bundle = new Bundle();
						bundle.putString("OpenMode", "ReadOnly");// 只读模式
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.setAction(android.content.Intent.ACTION_VIEW);
						intent.setClassName("cn.wps.moffice_eng",
								"cn.wps.moffice.documentmanager.PreStartActivity2");
						Uri uri = Uri.fromFile(new File(sdcard2));
						Log.e("wps访问的uri", uri+"");
						intent.setData(uri);
						intent.putExtras(bundle);
						try {
							startActivity(intent);
						} catch (ActivityNotFoundException e) {
							e.printStackTrace();
						} 
					}else{
						//返回真实路径
						mSavePath = d.downloadFile(filename);
						if(mSavePath!=""){
							Intent intent = new Intent();
							Bundle bundle = new Bundle();
							bundle.putString("OpenMode", "ReadOnly");// 只读模式
							intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							intent.setAction(android.content.Intent.ACTION_VIEW);
							intent.setClassName("cn.wps.moffice_eng",
									"cn.wps.moffice.documentmanager.PreStartActivity2");
							Uri uri = Uri.fromFile(new File(mSavePath+DownLoadUtil.urldecodeUTF8(filename.substring(filename.lastIndexOf("/")+1,filename.length()))));
							Log.e("wps访问的uri", uri+"");
							intent.setData(uri);
							intent.putExtras(bundle);
							try {
								startActivity(intent);
							} catch (ActivityNotFoundException e) {
								e.printStackTrace();
							} 
						}else{
							mDownloadDialog.show();
							Toast.makeText(PreceptInfoActivity.this, "下载中", Toast.LENGTH_SHORT).show();
						}
					}
				}else{
						// 从市场上下载
					 	 Uri uri = Uri.parse("market://details?id=cn.wps.moffice_eng");
	                    // 直接从指定网址下载
	                    Intent it = new Intent(Intent.ACTION_VIEW, uri); 
	                    startActivity(it); 
				}
			}
			
		}
	};

猜你喜欢

转载自blog.csdn.net/niuzaiwenjie/article/details/81081504