private String publicFilePath = new StringBuilder(Environment.getExternalStorageDirectory() .getAbsolutePath()) .append(File.separator).append("isoc_files").append(File.separator) .toString();
private void loadFile(String url, final String subjectId,final String filePostfix,final String filePostfixName) { Map<String, String> map = new HashMap<>(); map.put("orderId", subjectId); map.put("access_token", access_token_h5); Log.e("testweb", "url::"+url); Log.e("testweb", "subjectId::"+subjectId); Log.e("testweb", "access_token::"+access_token_h5); OkHttp3Util.doPost(url, map, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; try { is = response.body().byteStream();//以字节流的形式拿回响应实体内容 //文件 if(filePostfix.equals("xlsx")){ File file = new File(isExistDir(publicFilePath), filePostfixName + ".xlsx"); filePath = file.getAbsolutePath(); Log.e("testweb", filePath); fos = new FileOutputStream(file); }else if(filePostfix.equals("xls")){ File file = new File(isExistDir(publicFilePath), filePostfixName + ".xls"); filePath = file.getAbsolutePath(); Log.e("testweb", filePath); fos = new FileOutputStream(file); }else{ File file = new File(isExistDir(publicFilePath), filePostfixName + "."+filePostfix); filePath = file.getAbsolutePath(); Log.e("testweb", filePath); fos = new FileOutputStream(file); } while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) is.close(); if (fos != null) fos.close(); } // FileBrowserUtils.openFile(new File(filePath), TestWebviewActivity.this); if(filePostfix.equals("zip") || filePostfix.equals("rar")){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), filePath, Toast.LENGTH_SHORT).show(); } }); }else{ startActivity(getFileIntent(filePath)); } } }); }
public String isExistDir(String saveDir) throws IOException { // 下载位置 File downloadFile = new File(saveDir); if (!downloadFile.exists()) { downloadFile.mkdirs(); } String savePath = downloadFile.getAbsolutePath(); Log.e("savePath", savePath); return savePath; } private Intent getFileIntent(String Path) { File file = new File(Path); Intent intent = new Intent("android.intent.action.VIEW"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri; //Uri uri = Uri.fromFile(file);//解决FileUriExposedException if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".cake.fileProvider", new File(String.valueOf(file))); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//给目标文件临时授权,必需添加 } else { uri = Uri.fromFile(new File(String.valueOf(file))); } String type = getMIMEType(file); if (type.contains("pdf") || type.contains("vnd.ms-powerpoint") || type.contains("vnd.ms-word") || type.contains("vnd.ms-excel") || type.contains("text/plain") || type.contains("text/html")) { if (isInstall(this, "cn.wps.moffice_eng")) { intent.setClassName("cn.wps.moffice_eng", "cn.wps.moffice.documentmanager.PreStartActivity2"); intent.setData(uri); } else { intent.addCategory("android.intent.category.DEFAULT"); intent.setDataAndType(uri, type); } } else { intent.addCategory("android.intent.category.DEFAULT"); intent.setDataAndType(uri, type); } return intent; }
/** * 判断文件类型 */ private static String getMIMEType(File f) { String type = ""; String fName = f.getName(); /* 取得扩展名 */ String end = fName.substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase(); /* 依扩展名的类型决定MimeType */ if (end.equals("pdf")) { type = "application/pdf"; } else if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) { type = "audio/*"; } else if (end.equals("3gp") || end.equals("mp4")) { type = "video/*"; } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) { type = "image/*"; } else if (end.equals("apk")) { type = "application/vnd.android.package-archive"; } else if (end.equals("pptx") || end.equals("ppt")) { type = "application/vnd.ms-powerpoint"; } else if (end.equals("docx") || end.equals("doc")) { type = "application/vnd.ms-word"; } else if (end.equals("xlsx") || end.equals("xls")) { type = "application/vnd.ms-excel"; } else if (end.equals("txt")) { type = "text/plain"; } else if (end.equals("html") || end.equals("htm")) { type = "text/html"; } else { //如果无法直接打开,就跳出软件列表给用户选择 type = "*/*"; } return type; } private boolean isInstall(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; }