TShopping
標題:
Android Studio如何把檔案放入assets目錄下並copy複製到手機內部
[打印本頁]
作者:
woff
時間:
2020-7-27 20:27
標題:
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[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
1.png
(18.56 KB, 下載次數: 6)
下載附件
保存到相冊
Android Studio assets copy java
2020-7-27 20:07 上傳
參考文章
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