Android:将文件保存到SD卡

时间:2017-08-07 12:04:48

标签: java android file

这是我制作的第一个Android应用程序,所以我对所有内容的条款都有点迷失。

我试图将我/ raw /目录中的文件复制到SD卡的根目录。

目前, my (使用过Stackoverflow,我自己没有写完)代码如下所示:

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(许可被拒绝)"。

My Manifest看起来像这样:

<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卡根目录?

1 个答案:

答案 0 :(得分:3)

根据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();
}