Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Sdcard doesn't create / copy a file
    primarykey
    data
    text
    <p>I am having some problems copying a file from a folder to other folder; both are in SDCard.</p> <p>I want to copy from <code>/sdcard/folder1/file.db</code> to <code>/sdcard/folder1/folder2</code>(creating if doesn't exist)<code>/file.db</code></p> <p>AndroidManifest.xml</p> <pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt; </code></pre> <p>My code</p> <pre><code>public void realizarBackup() { String nombrebd = "BRSAndroid.db"; File sd = new File(Environment.getExternalStorageDirectory().getPath()); if (sd.exists() &amp;&amp; sd.canWrite()){ File bdoriginal = new File(sd.getAbsolutePath() + "/BRSAndroid/" + nombrebd); File directoriobackup = new File(sd.getAbsolutePath() + "/BRSAndroid/Backup/"); File backup = new File(sd.getAbsolutePath() + "/BSRAndroid/Backup/" + nombrebd); directoriobackup.mkdir(); // It creates this folder, this doesn't fail. if (directoriobackup.exists()) { try { if(backup.canRead()){ if(backup.canWrite()) backup.createNewFile(); else Log.e("Error", "No tiene permisos para escribir."); } else // STOPS HERE Log.e("Error", "No tiene permisos para leer."); } catch (IOException e1) { Log.e("Error", "Error al crear el fichero BRSAndroid en la carpeta de destino."); } if(backup.exists()) { try { InputStream entrada = new FileInputStream(bdoriginal); OutputStream salida = new FileOutputStream(backup); byte[] buf = new byte[1024]; int longitud; while((longitud = entrada.read(buf)) &gt; 0){ salida.write(buf, 0, longitud); } entrada.close(); salida.close(); } catch (Exception e){ Log.e("Error", "Error a la hora de hacerse el backup."); } } } else { Log.e("Error", "Error en la creación del directorio /Backup/."); } } } </code></pre> <p>What am I doing wrong? I get stuck right here and I can't go ahead.</p> <p>I have discovered that the backup hasn't got read permissions.</p> <p>LogCat log:</p> <pre><code>07-25 09:37:05.578: W/System.err(16602): java.io.IOException: open failed: ENOENT (No such file or directory) 07-25 09:37:05.578: W/System.err(16602): at java.io.File.createNewFile(File.java:940) 07-25 09:37:05.586: W/System.err(16602): at NS.Android.Clientes.ClientesActivity.realizarBackup(ClientesActivity.java:1602) 07-25 09:37:05.586: W/System.err(16602): at NS.Android.Clientes.ClientesActivity$4.onClick(ClientesActivity.java:398) 07-25 09:37:05.586: W/System.err(16602): at android.view.View.performClick(View.java:4222) 07-25 09:37:05.586: W/System.err(16602): at android.view.View$PerformClick.run(View.java:17337) 07-25 09:37:05.594: W/System.err(16602): at android.os.Handler.handleCallback(Handler.java:615) 07-25 09:37:05.594: W/System.err(16602): at android.os.Handler.dispatchMessage(Handler.java:92) 07-25 09:37:05.594: W/System.err(16602): at android.os.Looper.loop(Looper.java:137) 07-25 09:37:05.594: W/System.err(16602): at android.app.ActivityThread.main(ActivityThread.java:4895) 07-25 09:37:05.594: W/System.err(16602): at java.lang.reflect.Method.invokeNative(Native Method) 07-25 09:37:05.601: W/System.err(16602): at java.lang.reflect.Method.invoke(Method.java:511) 07-25 09:37:05.601: W/System.err(16602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 07-25 09:37:05.601: W/System.err(16602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 07-25 09:37:05.601: W/System.err(16602): at dalvik.system.NativeStart.main(Native Method) 07-25 09:37:05.609: W/System.err(16602): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 07-25 09:37:05.609: W/System.err(16602): at libcore.io.Posix.open(Native Method) 07-25 09:37:05.609: W/System.err(16602): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 07-25 09:37:05.609: W/System.err(16602): at java.io.File.createNewFile(File.java:933) 07-25 09:37:05.617: W/System.err(16602): ... 13 more </code></pre> <h2>Solved changing path from File to String.</h2> <pre><code>String ruta = Environment.getExternalStorageDirectory().getPath()+"/BRSAndroid/" String nombrebd = "BRSAndroid.db"; File bdoriginal = new File(ruta + nombrebd); File backup = new File(ruta + "Backup/" + nombrebd); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload