Android怎么獲取drawable目錄圖片并存入指定文件

這篇文章給大家分享的是有關(guān)Android怎么獲取drawable目錄圖片并存入指定文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

在成都做網(wǎng)站、網(wǎng)站制作過程中,需要針對(duì)客戶的行業(yè)特點(diǎn)、產(chǎn)品特性、目標(biāo)受眾和市場情況進(jìn)行定位分析,以確定網(wǎng)站的風(fēng)格、色彩、版式、交互等方面的設(shè)計(jì)方向。成都創(chuàng)新互聯(lián)還需要根據(jù)客戶的需求進(jìn)行功能模塊的開發(fā)和設(shè)計(jì),包括內(nèi)容管理、前臺(tái)展示、用戶權(quán)限管理、數(shù)據(jù)統(tǒng)計(jì)和安全保護(hù)等功能。

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

第一步:獲取存儲(chǔ)的路徑 我們用/sdcard/Android/data/包名/的路徑 方便我們測試查看

 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();
 }

感謝各位的閱讀!關(guān)于“Android怎么獲取drawable目錄圖片并存入指定文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章標(biāo)題:Android怎么獲取drawable目錄圖片并存入指定文件
鏈接分享:http://muchs.cn/article48/pgdeep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、App開發(fā)、企業(yè)網(wǎng)站制作營銷型網(wǎng)站建設(shè)、全網(wǎng)營銷推廣外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)