这个方法可以说是网上特别多,所以我不多说,放在这里的主要原因是为了保存这个方法,今后需要用到时方便用,
/
//**
- 保存图片的另一种方式
-
@param bitmap
*/private void saveTOSD(Bitmap bitmap) { // TODO Auto-generated method stub
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.i("TestFile", "SD card is not avaiable/writeable right now.");
return;
}String name = new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA))+ ".jpg";
FileOutputStream b = null;File file = new File("/sdcard/myImage/");
file.mkdirs();// 创建文件夹 String
fileName = "/sdcard/myImage/"+name;try {
b = new FileOutputStream(fileName);
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件 }
catch (FileNotFoundException e)
{
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
}
catch (IOException e){
e.printStackTrace();
}
}
}
还有一个方法:这个方法是保存图片,并且在相册中显示图片
public void saveImage(Bitmap bmp) {
File appDir = new File(Environment.getExternalStorageDirectory(), "Amu");
if(!appDir.exists()) {
appDir.mkdir();
}
String fileName = "Qiangzhi"+System.currentTimeMillis() + ".jpg";
File file = new File(appDir,fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
//保存图片同时显示在相册中
Toast.makeText(getApplicationContext(),"保存成功!",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(appDir);
intent.setData(uri);
getApplicationContext().sendBroadcast(intent);
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章