繁体   English   中英

在Android的内部存储专用文件夹中写入文件

[英]Write a file in internal storage specific folder in Android

我想在内部存储的特定文件夹中写入文件,创建fileoutputStream时出错。 下面是我的代码:

String filePath = ctx.getFilesDir().getPath().toString() +"/"+folderName;
Log.d("filePath",filePath);
file= new File(filePath, filename);
final FileOutputStream fileOutput = new FileOutputStream(file);

你可以这样走

File directory = new File("path_to_directory");
try {
if(!file.exists()) {
directory.createNewFile();
}
File dataFile = new File(directory, "Your File Name");
FileOutputStream stream = new FileOutputStream(dataFile, true); // true if append is required.
stream.write();
stream.flush()
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (null != stream) {
stream.close();
}

这里path_to_directory = Environment.getExternalStorageDirectory() + File.seperator + "FolderName";

要么

path_to_directory = ctx.getFilesDirectory() + File.seperator + "FolderName";

顺便说一句,您不能在根目录中创建一个文件夹到android的内部存储中。 肯定会给您IOException,因为没有root的android内部FS是只读的。 因此请注意。

谢谢,快乐编码:-)

好的,如果要从凌空保存(或下载并保存到特定文件夹),请在此处输入代码

DownloadManager.Request request = new DownloadManager.Request(
            downloadUri);

    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false).setTitle(notesName)
           // .setDescription("Something useful. No, really.")
            .setDestinationInExternalPublicDir("/YOUR PATH"+"/"+OTHER PATH, notesName)
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM