我尝试将文件从/ data复制到/ storage / sdcard2 / backup,但有例外:
java.io.FileNotFoundException: /storage/sdcard2/test/sauvegarde.mdb (Permission denied)
在我的AndroidManifest中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我的功能:
override fun saveFile(source: String, destination: String) {
try {
//File(source).copyTo(File(destination), true)
val buffer = ByteArray(8192)
val input = FileInputStream(source)
val out = FileOutputStream(destination)
input.use { fileinput ->
out.use {fileOut->
while (true){
val length = fileinput.read(buffer)
if (length<=0)
break
fileOut.flush()
fileOut.close()
}
}
input.close()
}
Log.d("success","yes")
}
catch (e : IOException){
Log.d("error :",e.toString())
}
}
测试许可:
Log.d("permission : ",ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE).toString())
Log.d("permission : ",ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE).toString())
返回:
2018-01-01 03:37:59.807 3943-3943/ch.apptitude.smarthome.tablet D/permission :: 0
2018-01-01 03:37:59.807 3943-3943/ch.apptitude.smarthome.tablet D/permission :: 0
我不明白为什么我不能
intent = Intent()
intent.setAction(Intent.ACTION_CREATE_DOCUMENT)
startActivityForResult(intent,1)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode==Activity.RESULT_OK){
if (data != null){
var test = data.getData()
Log.d("URI",test.toString())
}
}
}
这将导致我的应用程序崩溃