Android:将文件保存到SD卡
这是我第一次制作Android应用程序,所以我对所有内容的条款都有些遗憾。Android:将文件保存到SD卡
我试图将我在我的/ raw /目录中的文件复制到我的SD卡的根目录。
目前,我(使用#1,没有完全写我自己)的代码如下所示:
btnWriteSDFile.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try {
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "fileName.docx");
myFile.createNewFile();
Toast.makeText(v.getContext(),"Wrote line", Toast.LENGTH_SHORT).show();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWriter.append("testFile");
myOutWriter.close();
fOut.close();
Toast.makeText(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
我得到的错误: “打开失败; EACCES(拒绝)”。
我的清单看起来是这样的:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
澄清,以避免重复: 除了这个事实,我得到这个错误:我怎么能写一个/raw/file.docx文件到我的SD卡根?
根据this文档中,你不得不在屋里和直接添加使用权限<manifest>
标签
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
...
<application>
...
<activity>
...
</activity>
</application>
</manifest>
这样
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这应该用于复制文件
InputStream in = getResources().openRawResource(R.raw.fileNameYourGoingToCopy);
String path = Environment.getExternalStorageDirectory() + "/anyFolder" + File.separator + fileNameWithExtension;
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
谢谢!这将删除该位置的错误,但是如何将我的/raw/file.docx写入目录? –
@clanc我编辑过。检查它是否有效。这是这个答案的重复。 https://stackoverflow.com/a/19465224/5202960 –
工程就像一个魅力,感谢您的帮助! –
工作除了在(per答案),你还需要考虑[运行时权限](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it)。 – CommonsWare
[异常'打开失败:EACCES(权限被拒绝)'在Android上可能重复](https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android) –
谢谢,我也会包括这一点!我很想知道,我怎么把我的/raw/file.docx写到SD卡上? –