//下载的显示进度 private void showProgressDialog() { if (mDialog == null) { mDialog = new ProgressDialog(this); mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置风格为圆形进度条 mDialog.setMessage("正在加载 ,请等待..."); mDialog.setIndeterminate(false);//设置进度条是否为不明确 mDialog.setCancelable(true);//设置进度条是否可以按退回键取消 mDialog.setCanceledOnTouchOutside(false); mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { // TODO Auto-generated method stub mDialog = null; } }); mDialog.show(); } } private void closeProgressDialog() { if (mDialog != null) { mDialog.dismiss(); mDialog = null; } } public Intent getFileIntent(File file) { // Uri uri = Uri.parse("http://m.ql18.com.cn/hpf10/1.pdf"); Uri uri = Uri.fromFile(file); String type = getMIMEType(file); Log.i("tag", "type=" + type); Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(uri, type); return intent; } public void writeToSDCard(String fileName, InputStream input) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File directory = new File(Environment.getExternalStorageDirectory() + "/IDCardSDKCaller/"); File file = new File(directory, fileName); // if(file.exists()){ // Log.i("tag", "The file has already exists."); // return; // } try { FileOutputStream fos = new FileOutputStream(file); byte[] b = new byte[2048]; int j = 0; while ((j = input.read(b)) != -1) { fos.write(b, 0, j); } fos.flush(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Log.i("tag", "NO SDCard."); } } private 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")) { /* android.permission.INSTALL_PACKAGES */ 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 { // /*如果无法直接打开,就跳出软件列表给用户选择 */ type = "*/*"; } return type; }
下载功能加等待条
最新推荐文章于 2022-08-30 11:23:35 发布