Android 獲取drawable目錄圖片 并存入指定文件的步驟詳解
第一步:獲取存儲的路徑 我們用/sdcard/Android/data/
包名/的路徑 方便我們測試查看
String path=MyApplication.getContextObject().getExternalFilesDir("").toString(); File file=new File(path);
第二步:根據(jù)該文件中存儲的路徑信息在文件系統(tǒng)上創(chuàng)建一個新的空文件
File finalImageFile = new File(file, System.currentTimeMillis() + ".jpg"); try { finalImageFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); }
第三步:將字節(jié)放入文件輸出流
FileOutputStream fos = null; try { fos = new FileOutputStream(finalImageFile); } catch (FileNotFoundException e) { e.printStackTrace(); }
第四步:將圖片壓縮成圖片格式
BitmapDrawable bitmapDrawable = (BitmapDrawable)MyApplication.getContextObject().getResources().getDrawable(R.drawable.account); Bitmap bitmap=bitmapDrawable.getBitmap(); if (bitmap == null) { Toast.makeText(MyApplication.getContextObject(), "圖片不存在",Toast.LENGTH_LONG).show(); return; } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); try { fos.flush(); fos.close(); Toast.makeText(MyApplication.getContextObject(), "圖片保存在:"+ finalImageFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); }
完整代碼
String path=MyApplication.getContextObject().getExternalFilesDir("").toString(); File file=new File(path); File finalImageFile = new File(file, System.currentTimeMillis() + ".jpg"); try { finalImageFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream fos = null; try { fos = new FileOutputStream(finalImageFile); } catch (FileNotFoundException e) { e.printStackTrace(); } BitmapDrawable bitmapDrawable = (BitmapDrawable)MyApplication.getContextObject().getResources().getDrawable(R.drawable.account); Bitmap bitmap=bitmapDrawable.getBitmap(); if (bitmap == null) { Toast.makeText(MyApplication.getContextObject(), "圖片不存在",Toast.LENGTH_LONG).show(); return; } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); try { fos.flush(); fos.close(); Toast.makeText(MyApplication.getContextObject(), "圖片保存在:"+ finalImageFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); }
總結(jié)
到此這篇關(guān)于Android 獲取drawable目錄圖片 并存入指定文件的文章就介紹到這了,更多相關(guān)android 目錄圖片存入指定文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中AsyncTask異步任務(wù)使用詳細(xì)實(shí)例(一)
AsyncTask是Android提供的輕量級的異步類,可以直接繼承AsyncTask,在類中實(shí)現(xiàn)異步操作,并提供接口反饋當(dāng)前異步執(zhí)行的程度(可以通過接口實(shí)現(xiàn)UI進(jìn)度更新),最后反饋執(zhí)行的結(jié)果給UI主線程,通過本文給大家介紹Android中AsyncTask異步任務(wù)使用詳細(xì)實(shí)例(一),需要的朋友參考下2016-02-02Android中ImageView.src設(shè)置圖片拉伸、填滿控件的方法
最近公司有個需求,要展示客戶公司的企業(yè)形象,用一張圖片放在ImageView中實(shí)現(xiàn),但是發(fā)現(xiàn)圖片并沒有填滿,而是在上下邊上留出了一點(diǎn)空白,下面這篇文章主要跟大家介紹了Android中ImageView.src設(shè)置圖片拉伸、填滿控件的方法,需要的朋友可以參考下。2017-06-06Android開發(fā)之滑動數(shù)值選擇器NumberPicker用法示例
這篇文章主要介紹了Android開發(fā)之滑動數(shù)值選擇器NumberPicker用法,結(jié)合實(shí)例形式分析了Android滑動數(shù)值選擇器NumberPicker的功能、相關(guān)函數(shù)、事件監(jiān)聽、界面布局等操作技巧,需要的朋友可以參考下2019-03-03Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例
今天小編就為大家分享一篇關(guān)于Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03用Android Location獲取當(dāng)前地理位置的方法
本篇文章小編為大家介紹,用Android Location獲取當(dāng)前地理位置的方法。需要的朋友參考下2013-04-04Android優(yōu)化查詢加載大數(shù)量的本地相冊圖片
本文介紹了Android優(yōu)化查詢加載大數(shù)量的本地相冊圖片,可以方便的照片的查詢,,感興趣的小伙伴們可以參考一下。2016-10-10Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片
這篇文章主要介紹了Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片的相關(guān)資料,需要的朋友可以參考下2016-02-02