這篇文章主要講解了“Android中如何讀寫(xiě)assets目錄中的資源文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Android中如何讀寫(xiě)assets目錄中的資源文件”吧!
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比南昌縣網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式南昌縣網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋南昌縣地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
一、AssetManager讀取文件常用的幾個(gè)API
1.文件讀取方式
AssetManager.open(String filename),返回的是一個(gè)InputSteam類型的字節(jié)流,這里的filename必須是文件,而不能是文件夾,AssetManager打開(kāi) 資源文件的open方法是一個(gè)重載方法,可以添加一個(gè)打開(kāi)方式的int參數(shù),根據(jù)參數(shù)不同可做相應(yīng)操作。
2.資源文件是可以存在文件夾以及子目錄
public final String[]list(String path),返回當(dāng)前目錄下面的所有文件以及子目錄的名稱??梢酝ㄟ^(guò)遞歸遍歷整個(gè)文件目錄,實(shí)現(xiàn)所有資源文件的訪問(wèn)。String[] Array of strings, one for each asset. These file names are relative to 'path'. You can open the file by concatenating 'path' and a name in the returned string (via File) and passing that to open().
二、相關(guān)實(shí)現(xiàn)代碼
資源APK(A.apk)
具體實(shí)現(xiàn)代碼片段,由于使用系統(tǒng)權(quán)限,生成的路徑可以自己改一下B.apk
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { ctxDealFile = this.createPackageContext("com.zlc.ipanel", Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { String uiFileName = "ipanelJoin"; deepFile(ctxDealFile, uiFileName); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); textView.setText("file is wrong"); } } }); // } public void deepFile(Context ctxDealFile, String path) { try { String str[] = ctxDealFile.getAssets().list(path); if (str.length > 0) {//如果是目錄 File file = new File("/data/" + path); file.mkdirs(); for (String string : str) { path = path + "/" + string; System.out.println("zhoulc:\t" + path); // textView.setText(textView.getText()+"\t"+path+"\t"); deepFile(ctxDealFile, path); path = path.substring(0, path.lastIndexOf('/')); } } else {//如果是文件 InputStream is = ctxDealFile.getAssets().open(path); FileOutputStream fos = new FileOutputStream(new File("/data/" + path)); byte[] buffer = new byte[1024]; int count = 0; while (true) { count++; int len = is.read(buffer); if (len == -1) { break; } fos.write(buffer, 0, len); } is.close(); fos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
感謝各位的閱讀,以上就是“Android中如何讀寫(xiě)assets目錄中的資源文件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Android中如何讀寫(xiě)assets目錄中的資源文件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
當(dāng)前名稱:Android中如何讀寫(xiě)assets目錄中的資源文件
本文路徑:http://sd-ha.com/article24/jicpce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、全網(wǎng)營(yíng)銷推廣、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站制作、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)