在 android 中写入 SD 卡总是返回 False
Writing to SD Card in android always returning False
大家好,我必须使用下面的代码将我的 sqlite 数据库复制到下载。但是如果 (sd.canWrite()) 总是返回 false... 我坚持这个,是的,我已经添加到我的清单中了。我在真实设备而不是虚拟设备上进行调试。求助
private void copyDbToExternal(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()){
String currentDBPath = "//data//data//" + context.getApplicationContext().getPackageName() + "//databases//"
+ "qamatrisdb";
String backupDBPath = "qamatrisdb";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
您的代码与SD卡无关。您的代码正在写入 external storage, not removable storage.
如果 canWrite()
正在返回 false
,您可能缺少 WRITE_EXTERNAL_STORAGE
权限,或者您可能将 targetSdkVersion
设置为 23 或更高并且没有处理 Android 6.0+ 运行时权限。
此外,请将您现有的 currentDBPath
逻辑替换为 a call to getDatabasePath()
。
您是否尝试使用 Dev.Android 上的教程?
http://developer.android.com/training/basics/data-storage/files.html
访问外部存储的示例:
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
大家好,我必须使用下面的代码将我的 sqlite 数据库复制到下载。但是如果 (sd.canWrite()) 总是返回 false... 我坚持这个,是的,我已经添加到我的清单中了。我在真实设备而不是虚拟设备上进行调试。求助
private void copyDbToExternal(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()){
String currentDBPath = "//data//data//" + context.getApplicationContext().getPackageName() + "//databases//"
+ "qamatrisdb";
String backupDBPath = "qamatrisdb";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
您的代码与SD卡无关。您的代码正在写入 external storage, not removable storage.
如果 canWrite()
正在返回 false
,您可能缺少 WRITE_EXTERNAL_STORAGE
权限,或者您可能将 targetSdkVersion
设置为 23 或更高并且没有处理 Android 6.0+ 运行时权限。
此外,请将您现有的 currentDBPath
逻辑替换为 a call to getDatabasePath()
。
您是否尝试使用 Dev.Android 上的教程?
http://developer.android.com/training/basics/data-storage/files.html
访问外部存储的示例:
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}