我得到的文件的上级目录不可写一个SD卡出口 [英] I get Parent directory of file is not writable for an SDCard export

查看:97
本文介绍了我得到的文件的上级目录不可写一个SD卡出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
    private Context ctx;

    /**
     *
     */
    public ExportDatabaseFileTask(Context ctx) {
        super();
        this.ctx = ctx;
    }

    // automatically done on worker thread (separate from UI thread)
    protected Boolean doInBackground(final String... args) {

        File dbFile = new File(Environment.getDataDirectory()
                + "/data/com.mypkg/databases/log.db");

        File exportDir = new File(Environment.getExternalStorageDirectory(), "");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }
        File file = new File(exportDir, dbFile.getName());

        try {
            file.createNewFile();//*
            this.copyFile(dbFile, file);
            return true;
        } catch (IOException e) {
            Log.e("mytag", e.getMessage(), e);
            return false;
        }
    }

    // can use UI thread here
    protected void onPostExecute(final Boolean success) {
        if (success) {
            Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();
        }
    }

    void copyFile(File src, File dst) throws IOException {
        FileChannel inChannel = new FileInputStream(src).getChannel();
        FileChannel outChannel = new FileOutputStream(dst).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }

}

在的标记线

file.createNewFile();

我得到 java.io.IOException异常:文件的上级目录不可写:/sdcard/log.db

我安装了SD卡,我可以轻松地将文件复制到它。可能是什么问题?

I have an sdcard installed and I can easily copy files to it. What might be wrong?

推荐答案

只是当有人七嘴八舌它犯错

Just when someone rushes it makes mistakes

这是一个缺少权限

<uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

这篇关于我得到的文件的上级目录不可写一个SD卡出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆