App應用越來越人性化,不僅界面優美而且服務也很多樣化,操作也非常方便。比如我們在用app的時候,發現上面有比較的圖片想保存到手機,只要點一點app上提供的保存按鈕就可以了。那這個圖片保存到本地怎么實現的呢?
保存圖片很簡單,方法如下:
/** 首先默認個文件保存路徑 */private static final String SAVE_PIC_PATH=Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() : /mnt/sdcard;//保存到SD卡private static final String SAVE_REAL_PATH = SAVE_PIC_PATH+ /good/savePic;//保存的確切位置
下面就是保存的方法,傳入參數就可以了:
public static void saveFile(Bitmap bm, String fileName, String path) throws IOException {String subForder = SAVE_REAL_PATH + path;File foder = new File(subForder);if (!foder.exists()) {foder.mkdirs();}File myCaptureFile = new File(subForder, fileName);if (!myCaptureFile.exists()) {myCaptureFile.createNewFile();}www.49028c.comBufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);bos.flush();bos.close();}
這樣就保存好了,可是有的時候明明保存下來了,為什么進入相冊時查看不到呢?反正我是遇到這樣的問題的,原來我們在保存成功后,還要發一個系統廣播通知手機有圖片更新,廣播如下:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri = Uri.fromFile(file);intent.setData(uri);context.sendBroadcast(intent);//這個廣播的目的就是更新圖庫,發了這個廣播進入相冊就可以找到你保存的圖片了!,記得要傳你更新的file哦
以上內容是基于Android實現保存圖片到本地并可以在相冊中顯示出來的全部敘述,希望能夠幫助大家。
新聞熱點
疑難解答
圖片精選