private final static String ALBUM_PATH = Environment .getExternalStorageDirectory() + "/image/"; private List<String> filenames = new ArrayList<String>(); private void createFileName(String path) { String[] arrpath = path.split("/"); File sdcardfile; String strPath = ""; for (int i = 1; i < arrpath.length; i++) { strPath = strPath + "/" + arrpath[i]; sdcardfile = new File(strPath); if (!sdcardfile.exists()) { sdcardfile.mkdir(); } } } private void readFileName() { File sdcardfile = new File(ALBUM_PATH); if (!sdcardfile.exists()) { createFileName(ALBUM_PATH); } File[] files = sdcardfile.listFiles(); for (File file : files) { if (file.isDirectory()) { this.readFileName(); shareJson(template_id, mSize, mType, maxValue, oName); } else { String filepath = file.getAbsolutePath(); if (filepath.endsWith("jpg") || filepath.endsWith("gif") || filepath.endsWith("bmp") || filepath.endsWith("png")) { filenames.add(filepath); } } } } private void DeleteFile(String filepath) { File file = new File(filepath); if (file.exists()) { file.delete(); } } public void saveFile(Bitmap bm, String fileName) throws IOException { File dirFile = new File(ALBUM_PATH); try { if (!dirFile.exists()) { dirFile.mkdir(); } File dirFileName = new File(ALBUM_PATH + fileName); if (!dirFileName.exists()) { File myCaptureFile = new File(ALBUM_PATH + fileName); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.PNG, 80, bos); Log.i("add", "success!"); bos.flush(); bos.close(); } else { Log.i("add", "the picture exists!"); } } catch (Exception e) { e.printStackTrace(); } } public void getData() { maxValue = 0.0; double temp; // String strType = String.valueOf(size) + "_" + String.valueOf(type); for (int i = 0; i < filenames.size(); i++) { String filepath = filenames.get(i).toString(); String fileName = filepath.substring(filepath.lastIndexOf('/') + 1, filepath.length());// 提取下载图片的文件名 Log.i("fileName", fileName); // if (!strType.equals(fileName // .substring(0, fileName.lastIndexOf('_')))) { // continue; // } String ImageVersion = fileName.substring( fileName.lastIndexOf('_') + 1, fileName.length()); Log.i("ImageVersion", ImageVersion); String template_id_get = fileName.substring( fileName.lastIndexOf('_') - 1, fileName.lastIndexOf('_')); Log.i("template_id_get", template_id_get); template_id = Integer.parseInt(template_id_get); temp = Double.valueOf(ImageVersion.substring(0, ImageVersion.lastIndexOf('.'))); arr = fileName.split("_"); Log.i("arr", "" + arr[1]);// 画面类型 arr_type.add(arr[1]); Log.i("template_id", "" + template_id); oName = arr[0]; // meiqi 、youshang 、jiayun Log.i("oName", "" + oName); if (maxValue < temp) maxValue = temp; String strType = oName + "_" + String.valueOf(oType) + "_" + String.valueOf(oSize) + "_" + template_id + "_" + maxValue; Log.i("strType", "" + strType); if (!strType .equals(fileName.substring(0, fileName.lastIndexOf('.')))) { maxValue = 1.0; continue; }else{ break; } } if (!arr_type.contains(oType)) { template_id = 0; oName = ""; maxValue = 1.0; } Log.i("template_id", "" + template_id); }
AndroidManifest.xml
添加:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-sdk android:minSdkVersion="7"></uses-sdk>