从资产到sdcard -open的android复制数据库失败:EISDIR(是一个目录)

时间:2014-07-13 11:31:43

标签: java android

这是我将数据库从资产文件夹复制到SD卡的代码:

File databaseFile = new File(context.getExternalFilesDir(null),"");
if(!databaseFile.exists()){
    databaseFile.mkdirs();
}

String outFileName = context.getExternalFilesDir(null) + "/db.db";
try {
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    InputStream myInput = context.getAssets().open("db");
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myInput.close();

    myOutput.flush();
    myOutput.close();
} catch (Exception e) {
    Log.v("this",e.getMessage().toString());
}

当我运行它时,它给了我这个错误:

/storage/emulated/0/Android/data/myPackageName/files/db.db: open failed: EISDIR (Is a directory)

我该如何解决这个问题? 我已经阅读过这个主题,但没有奏效: FileOutputStream crashes with "open failed: EISDIR (Is a directory)" error when downloading image

另外,我在读取设备上测试它,同样的错误 谢谢

2 个答案:

答案 0 :(得分:5)

我无法从您附加的日志行中获得完整的图片。

但是,如果我不得不猜测,你的问题可能就在这里:

  if(!databaseFile.exists()){
            databaseFile.mkdirs();
  }

请记住:mkdirs()接受传递它的整个路径参数,打破它,如果需要,还会创建新文件夹。 mkdirs()无法告诉目录中的文件

所以,如果你这样调用它:

databaseFile.mkdirs("/sdcard/rootDir/resDir/myImage.png");

它将创建一个名为myImage.png的文件夹。

请检查您的代码并根据需要进行更改。

答案 1 :(得分:0)

这是我用来将资产中的所有文件复制到SD卡的代码。这是一个异步任务,所以你想要实现某种响应方法来知道代码何时完成以及db是否可用。

变量maindir是您要复制到的文件夹位置

并记得在清单文件中提供承诺

public class copyEveStaticDataDump extends AsyncTask<String,Void,String> {
    private Context context;

    private String url,filename;
    private String maindir;
    public copyEveStaticDataDump(Context contextt,String maindir)  {
        super();
        this.context = contextt;
        this.maindir = maindir;



    }

    @Override
     protected String doInBackground(String... params) {
        copyFilesToSdCard();
        return null;
    }
    private void copyFilesToSdCard() {
        copyFileOrDir(""); // copy all files in assets folder in my project
    }

    private void copyFileOrDir(String path) {
        AssetManager assetManager = context.getAssets();
        String assets[] = null;
        try {
            Log.i("tag", "copyFileOrDir() "+path);
            assets = assetManager.list(path);

            if (assets.length == 0) {
                copyFile(path);
            } else {
                String fullPath =  maindir;
                Log.i("tag", "path="+fullPath);
                File dir = new File(fullPath);
                if (!dir.exists() && !path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                    if (!dir.mkdirs())
                        Log.i("tag", "could not create dir "+fullPath);
                for (int i = 0; i < assets.length; ++i) {
                    String p;
                    if (path.equals(""))
                        p = "";
                    else
                        p = path + "/";

                    if (!path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                        copyFileOrDir( p + assets[i]);
                }
            }
        } catch (IOException ex) {
            Log.e("tag", "I/O Exception", ex);
        }
    }

    private void copyFile(String filename) {
        AssetManager assetManager = context.getAssets();

        InputStream in = null;
        OutputStream out = null;
        String newFileName = null;
        try {
            Log.i("tag", "copyFile() "+filename);
            in = assetManager.open(filename);
            if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
                newFileName = maindir + filename.substring(0, filename.length()-4);
            else
                newFileName = maindir+"/" + filename;
            out = new FileOutputStream(newFileName);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Log.e("tag", "Exception in copyFile() of "+newFileName);
            Log.e("tag", "Exception in copyFile() "+e.toString());
        }

    }
    @Override
    protected void onPostExecute(String s) {

        super.onPostExecute(s);
    }
}