最近想利用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[4096];
- 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
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物聯網
|