我想将捕获的图像保存在 SD 卡特定文件夹中,并使用图像的当前时间研磨名称。
我怎样才能做到这一点?
获取位图对象后试试这个
private void save_Image_to_sdcard() {
// TODO Auto-generated method stub
OutputStream file_outputstream = null;
InputStream in = null;
try {
URL path = new URL(url);
in = path.openStream();
String path1 =
Environment.getExternalStorageDirectory().toString();
System.out.println(path1+ " " +user_file_name);
// /mnt/sdcard
File f = new File(path1 + "/"+System.currentTimeMillis()+".png");
f.createNewFile();
System.out.println("file created " + f.toString());
file_outputstream = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = in.read(buffer, 0, buffer.length)) >= 0) {
file_outputstream.write(buffer, 0, bytesRead);
}
file_outputstream.close();
in.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("hey", "error");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("hey", " ioexception");
}
}
private Uri saveToFileAndUri() throws Exception{
long currentTime = System.currentTimeMillis();
String fileName = "MY_APP_" + currentTime+".jpg";
File extBaseDir = Environment.getExternalStorageDirectory();
File file = new File(extBaseDir.getAbsoluteFile()+"/MY_DIRECTORY");
if(!file.exists()){
if(!file.mkdirs()){
throw new Exception("Could not create directories, "+file.getAbsolutePath());
}
}
String filePath = file.getAbsolutePath()+"/"+fileName;
FileOutputStream out = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, out); //control the jpeg quality
out.flush();
out.close();
long size = new File(filePath).length();
ContentValues values = new ContentValues(6);
values.put(Images.Media.TITLE, fileName);
// That filename is what will be handed to Gmail when a user shares a
// photo. Gmail gets the name of the picture attachment from the
// "DISPLAY_NAME" field.
values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.Media.DATE_ADDED, currentTime);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, 0);
values.put(Images.Media.DATA, filePath);
values.put(Images.Media.SIZE, size);
return ThisActivity.this.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
}
按以下方式保存文件,
fileName + DateFormat.format( ConstantCodes.dd_MMM_yyyy_kk_mm_ss ,new java.util.Date()).toString() + ".jpg";