Android中怎么讀寫(xiě)assets目錄中的資源文件,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、當(dāng)陽(yáng)ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的當(dāng)陽(yáng)網(wǎng)站制作公司
一、AssetManager讀取文件常用的幾個(gè)API
1.文件讀取方式
AssetManager.open(String filename),返回的是一個(gè)InputSteam類(lèi)型的字節(jié)流,這里的filename必須是文件,而不能是文件夾,AssetManager打開(kāi) 資源文件的open方法是一個(gè)重載方法,可以添加一個(gè)打開(kāi)方式的int參數(shù),根據(jù)參數(shù)不同可做相應(yīng)操作。具體請(qǐng)看官方文檔http://web.mit.edu/clio/MacData/afs/sipb/project/android/docs/reference/android/content/res/AssetManager.html
2.資源文件是可以存在文件夾以及子目錄
public final String[]list(String path),返回當(dāng)前目錄下面的所有文件以及子目錄的名稱(chē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(); } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
文章名稱(chēng):Android中怎么讀寫(xiě)assets目錄中的資源文件
標(biāo)題網(wǎng)址:http://www.rwnh.cn/article32/jsdcpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、網(wǎng)站制作、微信小程序、面包屑導(dǎo)航、Google、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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)