加载我的应用程序时,它将一些原始jpg资源复制到内部存储中。这可以在“名称”字段中使用,但是我丢失了扩展名(.jpg)。如何使用包含扩展名的文件名来做到这一点?
package com.jasoncavinder.demoapp
import android.app.Application
import android.content.Context
import android.util.Log
import java.io.File
import com.jasoncavinder.demoapp.R
class DemoApplication : Application() {
private val TAG by lazy { this::class.java.simpleName }
override fun onCreate() {
super.onCreate()
// Copy demo files to internal storage if they do not exist yet
var count = 0
for (field in R.raw::class.java.fields) {
// I would prefer .endsWith(".jpg") but ...
if (field.name.startsWith("prefix").
&& !File(field.name).exists()
) {
resources.openRawResource(field.getInt(field))
.copyTo(applicationContext.openFileOutput(field.name, Context.MODE_PRIVATE))
count = count.inc()
}
}
Log.d(TAG, "Copied %d picture to internal storage.".format(count))
}
}