0
我有這樣的代碼:我獲取文件的父目錄不可寫一個SD卡出口
public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
private Context ctx;
/**
*
*/
public ExportDatabaseFileTask(Context ctx) {
super();
this.ctx = ctx;
}
// automatically done on worker thread (separate from UI thread)
protected Boolean doInBackground(final String... args) {
File dbFile = new File(Environment.getDataDirectory()
+ "/data/com.mypkg/databases/log.db");
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
try {
file.createNewFile();//*
this.copyFile(dbFile, file);
return true;
} catch (IOException e) {
Log.e("mytag", e.getMessage(), e);
return false;
}
}
// can use UI thread here
protected void onPostExecute(final Boolean success) {
if (success) {
Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();
}
}
void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
}
論
file.createNewFile();
標線,我得到java.io.IOException: Parent directory of file is not writable: /sdcard/log.db
我有一個SD卡安裝,我可以輕鬆地將文件複製到它。什麼可能是錯的?
我昨天剛做了同樣的事情。我認爲有趣的是,如果你的目標是1.5,那麼這個問題就不會發生,但如果你的目標是1.6+,你必須獲得許可 – 2010-08-26 10:51:54