我需要创建一个可用于其他应用程序的文件,例如。txt;我试了很多方法,但都无济于事;文件被创建,但是文件管理器(例如total commander)看不到它;这里是:
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();
}
我命令了所有权限。谢谢!
Replace:
openFileOutput(file.getName(), MODE_WORLD_READABLE);
:
new FileOutputStream(file);