參考資料 ----
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- // 檢查內部儲存空間是否已存在檔案
- val file = File(this.filesDir, "mymusic.mp3")
- if(!file.exists()) {
- copyAssets()
- } else {
- Toast.makeText(this, "mymusic.mp3 已複製到私有儲存空間", Toast.LENGTH_LONG).show();
- }
- }
- ...
- ...
- // 複製 mymusic.mp3 至私有儲存空間
- private fun copyAssets() {
- val mScope = CoroutineScope(Job() + Dispatchers.IO)
- mScope.launch {
- try {
- val outfile = FileOutputStream(File(filesDir, "mymusic.mp3").path)
- val infile: InputStream = assets.open("mymusic.mp3")
- infile.copyTo(outfile)
- outfile.close()
- infile.close()
- } catch(err: IOException) {
- err.printStackTrace();
- }
- }
- }
相關筆記 ----
沒有留言:
張貼留言