如何在android中创建可用于其他应用程序的文件?
我需要为其他应用程序创建一个文件,例如.txt;我尝试了很多方法,但没有任何帮助;文件被创建,但文件管理器(例如总指挥官)看不到它;我有这样的:
如何在android中创建可用于其他应用程序的文件?
String filename = mTitle.getText().toString().trim() + ".txt";
String string = mContent.getText().toString().trim();
FileOutputStream outputStream;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
file.mkdirs();
file.setReadable(true);
try {
outputStream = openFileOutput(file.getName(), MODE_WORLD_READABLE);
outputStream.write(string.getBytes());
outputStream.close();
Toast.makeText(getApplicationContext(), file.getAbsolutePath(), Toast.LENGTH_LONG).show();
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{file.getAbsolutePath()},
null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.v("grokkingandroid",
"file " + path + " was scanned seccessfully: " + uri);
}
});
new SingleMediaScanner(this, file);
} catch (Exception e) {
e.printStackTrace();
}
我下令所有权限。 谢谢!
替换:
openFileOutput(file.getName(), MODE_WORLD_READABLE);
有:
new FileOutputStream(file);
我试试。现在,这个问题:java.io.FileNotFoundException:/storage/emulated/0/Download/titlexgxgzrRZZTZ.txt:打开失败:EACCES(权限被拒绝)。所有权限:写WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE – Georgi
@Georgi:试试http://*.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare
谢谢!我解决了这个问题:) – Georgi
https://*.com/questions/32789157/how-to-write-files-to-external-public-storage-in -android-so-that-they-are-visibl – CommonsWare
好的。我添加了一个媒体扫描仪,但它没有帮助,我不明白为什么? – Georgi