這篇文章將為大家詳細講解有關Android應用中怎么將圖片保存到本地相冊,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
首先是保存圖片到本地
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 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(); Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
以上就是保存圖片的方法,保存完畢之后就是要通知相冊刷新了,
在4.4中:
MediaScannerConnection.scanFile(this, new String[]{SAVE_REAL_PATH+ "/" + fileName}, null, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted(String path, Uri uri) { Log.e( "onScanCompleted: ", path); Log.e( "onScanCompleted: ", uri.toString()); } });
在4.4以上的是發送廣播來實現:
Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED); //這是刷新SD卡 // Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); // 這是刷新單個文件 Uri uri = Uri.fromFile(new File(SAVE_REAL_PATH)); intent.setData(uri); sendBroadcast(intent);
關于Android應用中怎么將圖片保存到本地相冊就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
標題名稱:Android應用中怎么將圖片保存到本地相冊-創新互聯
網站網址:http://vcdvsql.cn/article14/djgpde.html
成都網站建設公司_創新互聯,為您提供品牌網站制作、網站設計公司、品牌網站建設、網站制作、虛擬主機、網站內鏈
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯