TShopping

 找回密碼
 註冊
搜索
查看: 2180|回復: 0

[教學] Android Studio如何把檔案放入assets目錄下並copy複製到手機內部

[複製鏈接]
發表於 2020-7-27 20:27:04 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
最近想利用Assets把檔案封裝到APP內這樣就不需要網路下載安裝在手機內

代碼:
  1. public void updateTool() {
  2.         copyAssets("xxx.dms", "xxx");
  3.     }

  4.     private void copyAssets(final String assetPath, final String localPath) {
  5.         try {
  6.             final String home = mContext.getFilesDir().getPath();
  7.             Log.d("samba",home+ " to " + localPath );
  8.             File file = new File(home + "/" + localPath);
  9.             if(!file.exists()) {
  10.                 InputStream in = mContext.getAssets().open(assetPath);
  11.                 FileOutputStream out = new FileOutputStream(home + "/" + localPath);
  12.                 int read;
  13.                 byte[] buffer = new byte[4096];
  14.                 while ((read = in.read(buffer)) > 0) {
  15.                     out.write(buffer, 0, read);
  16.                 }
  17.                 out.close();
  18.                 in.close();

  19.                 file = new File(home + "/" + localPath);
  20.                 if (!file.setExecutable(true)) {
  21.                     Log.d("samba","Can't set executable bit on " + localPath);
  22.                 }
  23.             }
  24.         } catch (IOException e) {
  25.             throw new RuntimeException(e);
  26.         }
  27.     }
複製代碼


把xxx.dms檔案放到assets目錄下
並把assets目錄放到該app/src/main
這時app目錄下就會出現assets

Android Studio assets copy java

Android Studio assets copy java



參考文章
https://www.jianshu.com/p/5974fcf88170
https://stackoverflow.com/questions/18302603/where-to-place-the-assets-folder-in-android-studio


來源http://www.netyea.com
#網頁設計 #網站架設 #關鍵字優化 #網頁優化 #App程式設計 #AIOT物聯網



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-19 04:11 , Processed in 0.114468 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表