user3718930
user3718930

Reputation: 381

android-copying database from asset to sdcard -open failed: EISDIR (Is a directory)

this is my code for copying database from asset folder to SD card:

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

when I run it ,it gives me this error :

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

How can I solve this ? I've read this topic but didn't work : FileOutputStream crashes with "open failed: EISDIR (Is a directory)" error when downloading image

also ,I test it on read device, the same error thank you

Upvotes: 1

Views: 4084

Answers (2)

fantazituborg
fantazituborg

Reputation: 17

This is the code I'm using to copy all files in assets to a sdcard. It's a asynch task so you want to implement some kind of respond method to know when the code is done and the db is useable.

The variable maindir is the folder location that you're copying to

and remember to give promissions in manifest file

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);
    }
}

Upvotes: 0

Gilad Haimov
Gilad Haimov

Reputation: 5857

I cannot get the full picture from the log line you have attached.

Still, if I had to guess, your problem is probably here:

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

Remember: mkdirs() takes the entire path param you pass it, breaks it and, if needed, creates new folders. mkdirs() cannot tell a file from a directory

So, if you invoke it like this:

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

It will create a folder named myImage.png.

Please check your code and change if needed.

Upvotes: 5

Related Questions