Android—将Bitmap图片保存到SD卡目录下或者指定目录
2016-03-13 00:17
555 查看
直接上代码就不废话啦
一:保存到SD卡下
二:保存Bitmap到指定的SD卡文件夹目录下
想想也知道,这个必须先有个文件夹,才能把图片保存这里面,那就按照思路学代码吧
注:访问权限注意哦
关于图片的 SD卡的
未完 待续
//将Bitmap图片保存到sd卡
protected void saveBitmapToSD(Bitmap bt) {
File path = Environment.getExternalStorageDirectory();
File file = new File(path, System.currentTimeMillis() + ".jpg");
System.out.println(Environment.getExternalStorageState() + "/Cool/" +"000000000000000000000000000");
try {
FileOutputStream out = new FileOutputStream(file);
bt.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
out.flush();
out.close();
}
/**
* 保存bitmap到SD卡
* @param bitName 保存的名字
* @param mBitmap 图片对像
* return 生成压缩图片后的图片路径
*/
public static String saveMyBitmap(String bitName,Bitmap mBitmap) {
File f = new File("/sdcard/" + bitName + ".png");
try {
f.createNewFile();
} catch (IOException e) {
System.out.println("在保存图片时出错:" + e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
} catch (Exception e) {
return "create_bitmap_error";
}
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
return "/sdcard/" + bitName + ".png";
}
/**
* 保存bitmap到SD卡
* @param bitmap
* @param imagename
*/
public static String saveBitmapToSDCard(Bitmap bitmap, String imagename) {
String path = "/sdcard/" + "img-" + imagename + ".jpg";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
if (fos != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
}
return path;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
一:保存到SD卡下
File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg"); try { out = new FileOutputStream(file); btImage.compress(Bitmap.CompressFormat.JPEG, 90, out); System.out.println("___________保存的__sd___下_______________________"); } catch (FileNotFoundException e) { e.printStackTrace(); } try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(HahItemActivity.this,"保存已经至"+Environment.getExternalStorageDirectory()+"下", Toast.LENGTH_SHORT).show(); }上面的方法就是 直接定义一个文件的路径,和图片的名称,然后保存.
二:保存Bitmap到指定的SD卡文件夹目录下
想想也知道,这个必须先有个文件夹,才能把图片保存这里面,那就按照思路学代码吧
if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) // 判断是否可以对SDcard进行操作 { // 获取SDCard指定目录下 String sdCardDir = Environment.getExternalStorageDirectory()+ "/CoolImage/"; File dirFile = new File(sdCardDir); //目录转化成文件夹 if (!dirFile .exists()) { //如果不存在,那就建立这个文件夹 dirFile .mkdirs(); } //文件夹有啦,就可以保存图片啦 File file = new File(sdCardDir, System.currentTimeMillis()+".jpg");// 在SDcard的目录下创建图片文,以当前时间为其命名 try { out = new FileOutputStream(file); btImage.compress(Bitmap.CompressFormat.JPEG, 90, out); System.out.println("_________保存到____sd______指定目录文件夹下____________________"); } catch (FileNotFoundException e) { e.printStackTrace(); } try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(HahItemActivity.this,"保存已经至"+Environment.getExternalStorageDirectory()+"/CoolImage/"+"目录文件夹下", Toast.LENGTH_SHORT).show(); }
注:访问权限注意哦
<!-- 安卓读写sd权限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
关于图片的 SD卡的
未完 待续
//将Bitmap图片保存到sd卡
protected void saveBitmapToSD(Bitmap bt) {
File path = Environment.getExternalStorageDirectory();
File file = new File(path, System.currentTimeMillis() + ".jpg");
System.out.println(Environment.getExternalStorageState() + "/Cool/" +"000000000000000000000000000");
try {
FileOutputStream out = new FileOutputStream(file);
bt.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
out.flush();
out.close();
}
/**
* 保存bitmap到SD卡
* @param bitName 保存的名字
* @param mBitmap 图片对像
* return 生成压缩图片后的图片路径
*/
public static String saveMyBitmap(String bitName,Bitmap mBitmap) {
File f = new File("/sdcard/" + bitName + ".png");
try {
f.createNewFile();
} catch (IOException e) {
System.out.println("在保存图片时出错:" + e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
} catch (Exception e) {
return "create_bitmap_error";
}
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
return "/sdcard/" + bitName + ".png";
}
/**
* 保存bitmap到SD卡
* @param bitmap
* @param imagename
*/
public static String saveBitmapToSDCard(Bitmap bitmap, String imagename) {
String path = "/sdcard/" + "img-" + imagename + ".jpg";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
if (fos != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
}
return path;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
相关文章推荐
- Android 中字符串的拆分--split的使用
- AndroidStudio下加入百度地图的使用(一)——环境搭建
- android中的文件(图片)上传
- Android Activity的4种启动模式详解(示例)
- 唤醒锁: 检测 Android* 应用中的 No-Sleep(无法进入睡眠)问题
- Android selector的item里的属性说明
- Servlet+msql+ android客户端实现从服务器获取信息
- 36个Android开发常用代码片段
- Android---获取手机联系人及归属地
- [android] 内容提供者实现
- 使用实验性的 Gradle 工具构建 Android NDK 环境
- Android开发之调用摄像头照相并剪切
- Android studio提示:plugs is too old
- This version of android studio is incompatible with the gradle version used.Try disabling the instant run
- Android布局文件.xml中的自定义属性
- android studio Cannot reload AVD list
- android 圆角图片
- 将android手机屏幕投影到电脑端
- android_________测绘流程
- Android -- XML Drawable