The second day cannot be done elementary: copy this elusive file. Here is the code:

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(getApplicationContext(), "SD-ΠΊΠ°Ρ€Ρ‚Π° Π½Π΅ доступна: " + Environment.getExternalStorageState(), Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "SD-ΠΊΠ°Ρ€Ρ‚Π° доступна: " + Environment.getExternalStorageState(), Toast.LENGTH_LONG).show(); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΏΡƒΡ‚ΡŒ ΠΊ SD File sdPath = getExternalFilesDir(null); // добавляСм свой ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ ΠΊ ΠΏΡƒΡ‚ΠΈ sdPath = new File(sdPath.getAbsolutePath()); // создаСм ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ if (!sdPath.exists()) { sdPath.mkdirs(); Toast.makeText(getApplicationContext(), "создали ΠΏΠ°ΠΏΠΊΡƒ", Toast.LENGTH_LONG).show(); } try { // ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΡ‚ΠΎΠΊΠΈ для записи File fFrom = new File(this.getDatabasePath("DBtimes.db").getAbsolutePath(), "DBtimes.db"); File fTo = new File(sdPath, "CH_" + s_now + ".db"); InputStream in = new FileInputStream(fFrom); OutputStream out = new FileOutputStream(fTo); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Π—Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ ΠΏΠΎΡ‚ΠΎΠΊΠΈ in.close(); out.close(); Toast.makeText(getApplicationContext(), s_now + "ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ Ρ„Π°ΠΉΠ» ", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException ex) { Toast.makeText(getApplicationContext(), "Π½Π΅ нашли Ρ„Π°ΠΉΠ»", Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(getApplicationContext(), "другая ошибка", Toast.LENGTH_LONG).show(); } 

What I just did not write here:

 File fFrom = new File(this.getDatabasePath("DBtimes.db").getAbsolutePath(), "DBtimes.db"); 

and

 File fFrom = new File("/data/data/имя_ΠΏΠ°ΠΊΠ΅Ρ‚Π°/databases/DBtimes.db"); 

and just

 File(getDatabasePath("DBtimes.db").getAbsolutePath(), "DBtimes.db"); 

And with the extension .db and without, and tried to copy the open database, and closed. It also doesn't work like this:

 File fFrom = new File(getApplicationContext().getDatabasePath("DBtimes.db").getAbsolutePath(), "DBtimes.db"); 

but

 Π‘ontext.getDatabasePath("DBtimes.db").getAbsolutePath(); 

Paints the word Π‘ontext red or otherwise writes that the non-static getDatabasePath ("DBtimes.db") cannot be used with the static Π‘ontext. What is wrong?

  • And what exactly does not work? Get the path to the database file or write it? .. Permissions to write / read files in the manifest are registered? What version of OS are you trying on? - Yuriy SPb ♦
  • @YuriiSPb Toast "did not find the file" from FileNotFoundException works. That is, can not take the database file. I wrote down the files to the address "where" and copied them - this is how I checked. But to take the database file does not work. It may be affected by the fact that it is in "@SuppressWarnings (" StatementWithEmptyBody ")" @Override public boolean onNavigationItemSelected (MenuItem item) {switch (item.getItemId ()) {case R.id.export: // data export ", but the text files from the place inside the destination folder are copied with this too. - St-st
  • OS, is not version 6 an hour? .. Does the file even exist in the folder? Those. Is it in the file list of the folder? - Yuriy SPb ♦
  • at 6.0. :) This method was read and tried. Nothing happens. I have not yet done checking the list of files, as you advised through an array, because I don’t know how to display it on the screen. - St-st

1 answer 1

I found a much more trivial way out: a direct reference to the address, but with two slashes.

  File fFrom = new File("//data/data/имя_ΠΏΠ°ΠΊΠ΅Ρ‚Π°/databases/", "DBtimes");