android儲存圖片到本地並可以在相簿中顯示出來

來源:互聯網
上載者:User

標籤: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儲存圖片到本地並可以在相簿中顯示出來

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.