生活随笔
收集整理的這篇文章主要介紹了
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))??????????????{?????????????????????String??sdCardDir?=?Environment.getExternalStorageDirectory()+?"/CoolImage/";???????????????????File?dirFile??=?new?File(sdCardDir);???????????????????if?(!dirFile?.exists())?{??????????????????????????????????dirFile?.mkdirs();???????????????????????}???????????????????????????????????????????File?file?=?new?File(sdCardDir,?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()+"/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卡的?
未完 ? 待續
?
[java]?view plain
?copy ?????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();?????}??????????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";?????}??????????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;?????}????
轉載于:https://www.cnblogs.com/exmyth/p/8428456.html
總結
以上是生活随笔為你收集整理的Android—将Bitmap图片保存到SD卡目录下或者指定目录的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。