TShopping

標題: Android Studio如何把檔案放入assets目錄下並copy複製到手機內部 [打印本頁]

作者: woff    時間: 2020-7-27 20:27
標題: Android Studio如何把檔案放入assets目錄下並copy複製到手機內部
最近想利用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



參考文章
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物聯網







歡迎光臨 TShopping (http://www.tshopping.com.tw/) Powered by Discuz! X3.2