user3853024
user3853024

Reputation: 11

Android/Java: copying database from /data/data to sdcard | Error: No such file or directory

My application copies a database from /data/data to the external storage into a subdirectory. Works well, but after some time it does not work any more.

I also tried with Stericson's RootTools library but there is the same error.

Error message:

 Executing: cp -fp /data/data/file.db /storage/emulated/0/Folder/19700820_135043.db
[ 08-20 13:50:43.559 1959: 2206 D/Command ]
ID: 0, cp: /storage/emulated/0/Folder/19700820_135043.db: No such file or directory

The following permissions are in manifest.xml:

android.permission.WRITE_EXTERNAL_STORAGE

android.permission.ACCESS_SUPERUSER

The strange things are:

My code is:

private class Async extends AsyncTask<Void, Void, Void> {
    
            homedir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder/";
            Fhomedir = new File(homedir);
            datadir = Environment.getDataDirectory().getAbsolutePath();
            sourcefile = "settings.db";
            homefile = "backup_" + currentDateandTime + ".db";
            sourcedir = datadir + "/data/com.android.providers.settings/databases/" + sourcefile;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
    
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                String currentDateandTime = sdf.format(new Date());
                homefile = "backup_" + currentDateandTime + ".db";
    
    
                try {
                    File file = new File(homedir, String.valueOf(System.currentTimeMillis() + ".txt"));
                    Runtime.getRuntime().exec("logcat -d -v long -f " + file.getAbsolutePath());
                }
                catch (IOException e){
                    e.printStackTrace();
                }
    
    
                Fhomedir = new File(homedir);
                if(!Fhomedir.exists()){
                    Fhomedir.mkdirs();
                }
    
                dialog.show();
            }
    
            @Override
            protected Void doInBackground(Void... voids) {
    
                if(RootTools.isAccessGiven()){
                    Log.e("ROOTACCESS", "TRUE");
                }
                else Log.e("ROOTACCESS", "FALSE");
    
                if(RootTools.remount(homedir, "rw")) {
                    if (isExternalStorageWritable()) {
                        //RootTools.copyFile(sourcedir, homedir + homefile, true, true);
                        FileUtils.copyToDirectory(sourcedir, homedir + homefile);
                        //exportDB();
                    }
                    else Toast.makeText(mContext, "Can't write to SDCARD", Toast.LENGTH_LONG).show();
                }
                else Toast.makeText(mContext, "Can't write to SDCARD",     Toast.LENGTH_LONG).show();
    
                return null;
            }
    
    
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
    
                Intent update = new Intent("com.flextrick.UPDATE");
                mContext.sendBroadcast(update);
                dialog.dismiss();
    
                File homefilex = new File(homedir + homefile);
                if(homefilex.exists()){
                    Toast.makeText(mContext, resources.getString(R.string.success_saved), Toast.LENGTH_LONG).show();
                }
                else Toast.makeText(mContext, resources.getString(R.string.failed), Toast.LENGTH_LONG).show();
            }
    
}

Upvotes: 0

Views: 1319

Answers (1)

user3853024
user3853024

Reputation: 11

I found an answer to my question: Somehow Android is avoiding new Files to be written to my folder on the SD-Card.

The method Environment.getExternalStorageDirectory returns /storage/emulated/0 as path. In my insight the permission WRITE_EXTERNAL_STORAGE takes only effect for the legacy path.

So the method to return the right path for writing to the external storage is Environment.getLegacyExternalStorageDirectory which returns /storage/emulated/legacy .. can vary for others of course.

Upvotes: 1

Related Questions