赋料扬雄敌,诗看子建亲。这篇文章主要讲述Android—将Bitmap图片保存到SD卡目录下或者指定目录相关的知识,希望能为你提供帮助。
直接上代码就不废话啦
一:保存到SD卡下
[java]
view plain
copy
- 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卡文件夹目录下
想想也知道,这个必须先有个文件夹,才能把图片保存这里面,那就按照思路学代码吧
[java] view plain copy
- 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();
- }
注:访问权限注意哦
[java] view plain copy
- < !-- 安卓读写sd权限 -->
- < uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- t; uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
关于图片的 SD卡的
未完 待续
【Android—将Bitmap图片保存到SD卡目录下或者指定目录】
[java] view plain copy
- //将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;
- }
推荐阅读
- linux的安全--Selinux,tcp_wrappers,iptables使用
- Android adb input 命令介绍
- Android截图命令screencap与视频录制命令screenrecord
- 停止win10浏览版自动安装新版本图文详细教程
- 微软win7与win10哪个好
- 处理win10升级报错代码1049图文详细教程
- 处理win10版office无法打开小技巧
- 一键处理Win10浏览版安装问题
- Win10 Build 10061升级图文详细教程