gpt4 book ai didi

android - canWrite() 到 SD 返回 false

转载 作者:搜寻专家 更新时间:2023-11-01 09:29:41 27 4
gpt4 key购买 nike

当我使用调试器时,sd.canWrite() 语句返回 false 并且不运行其余代码。我用模拟器和真正的三星平板电脑试过了。我有

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

在我的 list 中。

public void exportDatabse(String databaseName) {
try {
File sd = Environment.getExternalStorageDirectory();

File data = Environment.getDataDirectory();


if (sd.canWrite()) {
String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
String backupDBPath = "backupname.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
Log.d("file", "file copied2");

if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
Log.d("file", "file copied3");
}
} catch (Exception e) {
Log.d("file", "file not copied");

}
Log.d("file", "method runs");
}

最佳答案

请检查外部存储是否写保护-

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

}

关于android - canWrite() 到 SD 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48089815/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com