如何在 Android 中写入 SD 卡上的文件夹?
我使用以下代码从服务器下载文件,然后将其写入 SD 卡的根目录,一切正常:
package com.downloader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Environment;
import android.util.Log;
public class Downloader {
public void DownloadFile(String fileURL, String fileName) {
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(root, fileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
}
}
但是,使用 Environment.getExternalStorageDirectory();
意味着该文件将始终写入根 /mnt/sdcard
。是否可以指定某个文件夹来写入文件?
例如:/mnt/sdcard/myapp/downloads
I am using the following code to download a file from my server then write it to the root directory of the SD card, it all works fine:
package com.downloader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Environment;
import android.util.Log;
public class Downloader {
public void DownloadFile(String fileURL, String fileName) {
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(root, fileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
}
}
However, using Environment.getExternalStorageDirectory();
means that the file will always write to the root /mnt/sdcard
. Is it possible to specify a certain folder to write the file to?
For example: /mnt/sdcard/myapp/downloads
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将权限添加到 Android 清单
将此 WRITE_EXTERNAL_STORAGE 权限添加到您的应用程序清单中。
检查外部存储的可用性
您应该始终首先检查可用性。 有关外部存储的官方 Android 文档的片段。
使用 Filewriter
最后但并非最不重要的是忘记
FileOutputStream
并使用FileWriter
代替。有关该类的更多信息请参见FileWriter javadoc。您可能需要在此处添加更多错误处理以通知用户。Add Permission to Android Manifest
Add this WRITE_EXTERNAL_STORAGE permission to your applications manifest.
Check availability of external storage
You should always check for availability first. A snippet from the official android documentation on external storage.
Use a Filewriter
At last but not least forget about the
FileOutputStream
and use aFileWriter
instead. More information on that class form the FileWriter javadoc. You'll might want to add some more error handling here to inform the user.在这里找到答案 - http://mytechead.wordpress.com/2014/01/30/android-create-a-file-and-write-to-external-storage/
它说,
然后让我们使用这段代码实际上写入外部存储:
就是这样。如果现在访问 /sdcard/your-dir-name/ 文件夹,您将看到一个名为 - My-File-Name.txt 的文件,其中包含代码中指定的内容。
PS:-您需要以下权限-
Found the answer here - http://mytechead.wordpress.com/2014/01/30/android-create-a-file-and-write-to-external-storage/
It says,
and then let’s use this code to actually write to the external storage:
And this is it. If now you visit your /sdcard/your-dir-name/ folder you will see a file named - My-File-Name.txt with the content as specified in the code.
PS:- You need the following permission -
为了将文件下载到 SDCard 中的下载或音乐文件夹
并且不要忘记在清单中添加这些权限
In order to download a file to Download or Music Folder In SDCard
And dont forget to add these permission in manifest