直接上代碼就不廢話啦java
一:保存到SD卡下android
- 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();
- }
上面的方法就是 直接定義一個文件的路徑,和圖片的名稱,而後保存.this
二:保存Bitmap到指定的SD卡文件夾目錄下spa
想一想也知道,這個必須先有個文件夾,才能把圖片保存這裏面,那就按照思路學代碼吧.net
- 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();
- }
注:訪問權限注意哦orm
- <!-- 安卓讀寫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卡的
未完 待續blog
- //將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;
- }