如何创建可供 android 中的其他应用程序使用的文件?
How to create a file available to other apps in android?
我需要创建一个可供其他应用程序使用的文件,例如 .txt;我尝试了很多方法,但没有任何帮助;文件已创建,但文件管理器(例如总指挥)看不到它;我有这个:
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);
我需要创建一个可供其他应用程序使用的文件,例如 .txt;我尝试了很多方法,但没有任何帮助;文件已创建,但文件管理器(例如总指挥)看不到它;我有这个:
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);