WEB开发网
开发学院手机开发Android 开发 android保存图片到SD卡并以时间命名 阅读

android保存图片到SD卡并以时间命名

 2012-11-26 20:56:02 来源:WEB开发网   
核心提示: private void storeInSD(Bitmap bitmap1) { File file = new File(FILE_DIR); if (!file.exists()) { file.mkdir(); } File imageFile = new File(file, FileUtils.g

 private void storeInSD(Bitmap bitmap1) {
File file = new File(FILE_DIR);
if (!file.exists()) {
file.mkdir();
}
File imageFile = new File(file, FileUtils.getFileName() + ".png");
try {
imageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(imageFile);
bitmap1.compress(CompressFormat.PNG, 50, fos);

Toast.makeText(MainActivity.this, "保存成功!", 1);

fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "保存失败!", 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "保存失败!", 1);
}
}

public static String getCharacterAndNumber() {
String rel="";
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis());
rel = formatter.format(curDate);
return rel;
}

public static String getFileName() {
// mu
//String fileNameRandom = getCharacterAndNumber(8);
String fileNameRandom = getCharacterAndNumber();
return fileNameRandom;
}

Tags:android 保存 图片

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接