Android 4.4.4 在三星 S4 上保存到 SD
Android 4.4.4 Save to SD on Samsung S4
我正在尝试将文件保存到我的 SD,但无法保存,我什至尝试将应用程序移动到 SD 以查看是否可以。我真的不在乎它在哪里结束,但这不起作用:
String state = Environment.getExternalStorageState();
File filesDir;
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
filesDir = getExternalFilesDir(null);
Log.i(Utils.TAG, "We can read and write the media: " + filesDir.getAbsolutePath()); // This is the local on the phone
} else {
// Load another directory, probably local memory
filesDir = getFilesDir();
Log.i(Utils.TAG, "Load another directory, probably local memory: " + filesDir.getAbsolutePath());
}
try {
// Creates a trace file in the primary external storage space of the
// current application.
// If the file does not exists, it is created.
//File traceFile = new File(((Context)this).getExternalFilesDir(null), "TraceFile.txt"); //This one saves to the internal file folder
File traceFile = new File(filesDir, "TraceFile.txt");
Log.i(Utils.TAG, traceFile.getAbsolutePath());
if (!traceFile.exists())
traceFile.createNewFile();
// Adds a line to the trace file
BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, true /*append*/));
writer.write("This is a test trace file.");
writer.close();
// Refresh the data so it can seen when the device is plugged in a
// computer. You may have to unplug and replug the device to see the
// latest changes. This is not necessary if the user should not modify
// the files.
MediaScannerConnection.scanFile((Context)(this),
new String[] { traceFile.toString() },
null,
null);
} catch (IOException e) {
Log.i(Utils.TAG, "Unable to write to the TraceFile.txt file.");
}
但是,这给了我 SD 文件,但我无法写入:
public HashSet<String> getExternalMounts() {
final HashSet<String> out = new HashSet<String>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s = s + new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
// parse output
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec")) {
if (line.matches(reg)) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.startsWith("/"))
if (!part.toLowerCase(Locale.US).contains("vold"))
out.add(part);
}
}
}
}
return out;
}
Android 4.4 实施了许多 SD 卡限制,在此期间有(现在仍然有)很多应用程序因为 SD 卡写入限制问题而崩溃。你的代码本身对我来说似乎很好,但我相信它是 Android 4.4 的 SD 卡限制,确保它不起作用。如何解决这个问题(没有 root 访问权限)超出了我的范围。
Android 4.4+ 允许您写入可移动媒体,但只能在通过以下方法获得的 select 点中:
getExternalFilesDirs()
getExternalCacheDirs()
getExternalMediaDirs()
(这个是在 Android 5.0 IIRC 中添加的)
注意这些方法名称的复数形式。如果它们 return 2+ 个条目,则第二个和后续条目应该在可移动媒体上,在您的应用程序唯一的目录中。您可以在没有任何 <uses-permission>
元素的情况下读取和写入这些目录。
您还可以考虑存储访问框架(ACTION_OPEN_DOCUMENT
和 kin),以允许用户选择放置文件的位置,无论是在外部存储或可移动存储上还是 Google开车什么的。
我正在尝试将文件保存到我的 SD,但无法保存,我什至尝试将应用程序移动到 SD 以查看是否可以。我真的不在乎它在哪里结束,但这不起作用:
String state = Environment.getExternalStorageState();
File filesDir;
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
filesDir = getExternalFilesDir(null);
Log.i(Utils.TAG, "We can read and write the media: " + filesDir.getAbsolutePath()); // This is the local on the phone
} else {
// Load another directory, probably local memory
filesDir = getFilesDir();
Log.i(Utils.TAG, "Load another directory, probably local memory: " + filesDir.getAbsolutePath());
}
try {
// Creates a trace file in the primary external storage space of the
// current application.
// If the file does not exists, it is created.
//File traceFile = new File(((Context)this).getExternalFilesDir(null), "TraceFile.txt"); //This one saves to the internal file folder
File traceFile = new File(filesDir, "TraceFile.txt");
Log.i(Utils.TAG, traceFile.getAbsolutePath());
if (!traceFile.exists())
traceFile.createNewFile();
// Adds a line to the trace file
BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, true /*append*/));
writer.write("This is a test trace file.");
writer.close();
// Refresh the data so it can seen when the device is plugged in a
// computer. You may have to unplug and replug the device to see the
// latest changes. This is not necessary if the user should not modify
// the files.
MediaScannerConnection.scanFile((Context)(this),
new String[] { traceFile.toString() },
null,
null);
} catch (IOException e) {
Log.i(Utils.TAG, "Unable to write to the TraceFile.txt file.");
}
但是,这给了我 SD 文件,但我无法写入:
public HashSet<String> getExternalMounts() {
final HashSet<String> out = new HashSet<String>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s = s + new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
// parse output
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec")) {
if (line.matches(reg)) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.startsWith("/"))
if (!part.toLowerCase(Locale.US).contains("vold"))
out.add(part);
}
}
}
}
return out;
}
Android 4.4 实施了许多 SD 卡限制,在此期间有(现在仍然有)很多应用程序因为 SD 卡写入限制问题而崩溃。你的代码本身对我来说似乎很好,但我相信它是 Android 4.4 的 SD 卡限制,确保它不起作用。如何解决这个问题(没有 root 访问权限)超出了我的范围。
Android 4.4+ 允许您写入可移动媒体,但只能在通过以下方法获得的 select 点中:
getExternalFilesDirs()
getExternalCacheDirs()
getExternalMediaDirs()
(这个是在 Android 5.0 IIRC 中添加的)
注意这些方法名称的复数形式。如果它们 return 2+ 个条目,则第二个和后续条目应该在可移动媒体上,在您的应用程序唯一的目录中。您可以在没有任何 <uses-permission>
元素的情况下读取和写入这些目录。
您还可以考虑存储访问框架(ACTION_OPEN_DOCUMENT
和 kin),以允许用户选择放置文件的位置,无论是在外部存储或可移动存储上还是 Google开车什么的。