標籤:android應用 圖片
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();
}
BufferedOutputStream 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儲存圖片到本地並可以在相簿中顯示出來