Android—将Bitmap图片保存到SD卡目录下或者指定目录

直接上代码就不废话啦

:保存到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卡的 
未完   待续

 //将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)