woff 發表於 2020-7-27 20:27:04

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

最近想利用Assets把檔案封裝到APP內這樣就不需要網路下載安裝在手機內

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

    private void copyAssets(final String assetPath, final String localPath) {
      try {
            final String home = mContext.getFilesDir().getPath();
            Log.d("samba",home+ " to " + localPath );
            File file = new File(home + "/" + localPath);
            if(!file.exists()) {
                InputStream in = mContext.getAssets().open(assetPath);
                FileOutputStream out = new FileOutputStream(home + "/" + localPath);
                int read;
                byte[] buffer = new byte;
                while ((read = in.read(buffer)) > 0) {
                  out.write(buffer, 0, read);
                }
                out.close();
                in.close();

                file = new File(home + "/" + localPath);
                if (!file.setExecutable(true)) {
                  Log.d("samba","Can't set executable bit on " + localPath);
                }
            }
      } catch (IOException e) {
            throw new RuntimeException(e);
      }
    }

把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物聯網


頁: [1]
查看完整版本: Android Studio如何把檔案放入assets目錄下並copy複製到手機內部