[英]Android: how to write a file locally and save it to the Downloads
How do you write files locally and save them to the Downloads
App in Android? 您如何在本地编写文件并将其保存到Android中的“ Downloads
应用程序中?
Android version: Nougut
Android版本: Nougut
The file is not showing in Downloads
though. 不过,该文件未显示在“ Downloads
。 Here's my code: 这是我的代码:
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "foo.txt");
try {
String exampleString = "bar\nfoo";
InputStream is = new ByteArrayInputStream(exampleString.getBytes(Charset.forName("UTF-8")));
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
throw new AssertionError(e.toString());
}
MediaScannerConnection.scanFile(
getContext(),
new String[]{file.getAbsolutePath()},
null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String s, Uri uri) {
Log.d(TAG, "String: "+ s);
Log.d(TAG, "Uri: "+ uri );
}
});
It is logging this in onScanCompleted
so it seems like the file should show in Downloads
but it doesn't. 它正在onScanCompleted
记录此文件,因此该文件似乎应显示在Downloads
但实际上却没有。
D/SignupFragment: String: /storage/emulated/0/Download/foo.txt
D/SignupFragment: Uri: content://media/external/file/84691
I have read the Android docs on saving files 我已经阅读了有关保存文件的Android文档
AndroidManifest.xml AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The standard AOSP Downloads app only shows what DownloadManager
downloaded. 标准的AOSP下载应用程序仅显示DownloadManager
下载的内容。 It does not show files placed in the Downloads/
directory by other means. 它不显示通过其他方式放置在Downloads/
目录中的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.