第一件事OU要做的就是添加權限訪問您的清單文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
保存在內部存儲
File file = new File(context.getFilesDir(), filename);
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
public File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
保存文件的文件上的外部存儲
外部存儲
/* 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;
}
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
我不確定你認爲「應用程序文件夾」是什麼。您的硬編碼路徑與'Context'(例如「Activity」或「Service」)上調用的'getExtenalFilesDir(null)'的輸出極其相似。除此之外,請詳細解釋**「什麼是不起作用」的意思。 – CommonsWare
我不明白你爲什麼使用硬編碼路徑?你可以簡單地將文件保存在內部存儲中https://developer.android.com/training/basics/data-storage/files.html – petrumo
他的應用程序的目錄與我的不同,他的內容類似於數據/數據/包裹名字。應用程序無法將文件保存在該位置,因爲它無法找到它。 – Facosenpai