将图片保存到一个新的文件夹中

来源:互联网 发布:aso优化 app下载 编辑:程序博客网 时间:2024/05/16 12:15

今天要把项目中的图片保存到一个新建的文件夹中:

(1) 

创建新的文件夹


public void CreateFile() {


String status1 = Environment.getExternalStorageState();
if (status1.equals(Environment.MEDIA_MOUNTED)) {
//判断有没有sd卡

sDir = SDCARD_DIR;
} else {
sDir = NOSDCARD_DIR;
}


// 然后是创建文件夹
File destDir = new File(sDir);
if (!destDir.exists()) {
destDir.mkdirs();
}







}


然后就是图片的保存:

使用bitmap的压缩方法来把图片添加到输出流中,通过输出流来保存图片。

(2)

public void Save_Pic(String path,Bitmap bitmap){


File  file =new File(path);

if (file.exists()) {
file.delete();
}else {

try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

FileOutputStream  outputStream  =null;

try {
outputStream  =new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100, outputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

0 0