這篇文章主要講解了“Android中如何讀寫(xiě)assets目錄中的資源文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Android中如何讀寫(xiě)assets目錄中的資源文件”吧!
成都創(chuàng)新互聯(lián)公司是一家專注于成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)與策劃設(shè)計(jì),弋江網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:弋江等地區(qū)。弋江做網(wǎng)站價(jià)格咨詢:18982081108一、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)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
文章標(biāo)題:Android中如何讀寫(xiě)assets目錄中的資源文件-創(chuàng)新互聯(lián)
文章來(lái)源:http://chinadenli.net/article8/dhjjip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、云服務(wù)器、網(wǎng)站制作、響應(yīng)式網(wǎng)站、App設(shè)計(jì)
聲明:本網(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)
猜你還喜歡下面的內(nèi)容