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

编程入门 行业动态 更新时间:2024-10-26 14:29:33

(代码)Android 实现调用wps打开word<a href=https://www.elefans.com/category/jswz/34/1770955.html style=文档,打开pdf文档"/>

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

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

Button lookdoc;
lookdoc = (Button)findViewById(R.id.lookdoc);
lookdoc.setOnClickListener(looklisten);OnClickListener looklisten = new OnClickListener() {@Overridepublic 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); }}}};
private boolean isAvilible( Context context, String packageName ){final PackageManager packageManager = context.getPackageManager();// 获取所有已安装程序的包信息List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);for ( int i = 0; i < pinfo.size(); i++ ){if(pinfo.get(i).packageName.equalsIgnoreCase(packageName))return true;}return false;}

 

2019年12月30日    附上 DownLoadUtil

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.HttpURLConnection;
import java.MalformedURLException;
import java.URL;
import java.URLConnection;import android.os.Environment;
import android.util.Log;public class DownLoadUtil {/** sd卡目录路径 */private String sdcard;// public DownLoadUtil(String url)// {// this.urlstr = url;// //获取设备sd卡目录// this.sdcard = Environment.getExternalStorageDirectory() + "/";// urlcon = getConnection();// }public String downloadFile(String filename,String u) {try {// 判断SD卡是否存在,并且是否具有读写权限if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 获得存储卡的路径String sdpath = Environment.getExternalStorageDirectory() + "/";sdcard = sdpath + "download/";String finalUrl = u + filename;Log.e("finalUrl", finalUrl);URL url = new URL(finalUrl);URLConnection rulConnection = url.openConnection();HttpURLConnection conn = (HttpURLConnection) rulConnection;// 设定请求的方法为"POST",默认是GETconn.setRequestMethod("POST");conn.connect();// 创建输入流InputStream is = conn.getInputStream();File file = new File(sdcard);// 判断文件目录是否存在if (!file.exists()) {file.mkdir();}String filename2 = filename.substring(filename.lastIndexOf("/") + 1);File apkFile = new File(sdcard, urldecodeUTF8(filename2));// System.out.println(apkFile);FileOutputStream fos = new FileOutputStream(apkFile);// 写入到文件中int len = 2048;// 缓存byte buf[] = new byte[len];while ((len = is.read(buf)) != -1) {// 写入文件fos.write(buf, 0, len);}fos.close();is.close();}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}// 关闭下载对话框显示return sdcard;}public static String urlEncodeUTF8(String source) {String result = source;try {result = java.URLEncoder.encode(source, "utf-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return result;}public static String urldecodeUTF8(String source) {String result = source;try {result = java.URLDecoder.decode(source, "utf-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return result;}
}

 

更多推荐

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

本文发布于:2024-03-14 20:30:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1737267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   代码   Android   wps   word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!