Android 獲取drawable目錄圖片 并存入指定文件的步驟詳解
第一步:獲取存儲(chǔ)的路徑 我們用/sdcard/Android/data/包名/的路徑 方便我們測(cè)試查看
String path=MyApplication.getContextObject().getExternalFilesDir("").toString();
File file=new File(path);
第二步:根據(jù)該文件中存儲(chǔ)的路徑信息在文件系統(tǒng)上創(chuàng)建一個(gè)新的空文件
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Input系統(tǒng)截?cái)嗖呗缘姆治雠c應(yīng)用詳解
這篇文章主要為大家介紹了Input系統(tǒng)截?cái)嗖呗缘姆治雠c應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android下Activity間通信序列化過程中的深淺拷貝淺析
這篇文章主要給大家介紹了關(guān)于Android下Activity間通信序列化過程中深淺拷貝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Android實(shí)現(xiàn)讀取相機(jī)(相冊(cè))圖片并進(jìn)行剪裁
在 Android應(yīng)用中,很多時(shí)候我們需要實(shí)現(xiàn)上傳圖片,或者直接調(diào)用手機(jī)上的拍照功能拍照處理然后直接顯示并上傳功能,下面將講述調(diào)用相機(jī)拍照處理圖片然后顯示和調(diào)用手機(jī)相冊(cè)中的圖片處理然后顯示的功能2015-08-08
Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框(二)
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android利用控制點(diǎn)的拖拽畫一個(gè)粽子
端午節(jié)就要到了,本文我們將利用控制點(diǎn)的拖拽式移動(dòng),動(dòng)態(tài)調(diào)整位置來調(diào)整繪制一個(gè)簡(jiǎn)單的粽子圖形,感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-05-05

