Reputation: 1855
I am having some problems copying a file from a folder to other folder; both are in SDCard.
I want to copy from /sdcard/folder1/file.db
to /sdcard/folder1/folder2
(creating if doesn't exist)/file.db
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
My code
public void realizarBackup()
{
String nombrebd = "BRSAndroid.db";
File sd = new File(Environment.getExternalStorageDirectory().getPath());
if (sd.exists() && 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)) > 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/.");
}
}
}
What am I doing wrong? I get stuck right here and I can't go ahead.
I have discovered that the backup hasn't got read permissions.
LogCat log:
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
String ruta = Environment.getExternalStorageDirectory().getPath()+"/BRSAndroid/"
String nombrebd = "BRSAndroid.db";
File bdoriginal = new File(ruta + nombrebd);
File backup = new File(ruta + "Backup/" + nombrebd);
Upvotes: 1
Views: 2038
Reputation: 9666
This snippet doesn't really make sense:
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.");
Of course, you're not going to be able to read or write to a file that you haven't created yet! Did you mean to check after, like this?
backup.createNewFile(); // create, THEN check if you can read and write
if (!backup.canRead())
Log.e("Error", "No tiene permisos para leer.");
if (!backup.canWrite())
Log.e("Error", "No tiene permisos para escribir.");
Or, perhaps you meant to check if you can read bdoriginal
, or to check that you can read and write to directoriobackup
?
Upvotes: 0
Reputation: 2368
Hi use like this..
public void realizarBackup()
{
String nombrebd = "BRSAndroid.db";
File bdoriginal = new File(Environment.getExternalStorageDirectory() + "/BRSAndroid/" + nombrebd);
File directoriobackup = new File(Environment.getExternalStorageDirectory() + "/BRSAndroid/Backup/");
File backup = new File(Environment.getExternalStorageDirectory() + "/BSRAndroid/Backup/" + nombrebd);
if(!directoriobackup .exists())
{
directoriobackup .mkdir();
}
if (!backup.exists()) {
try {
backup.createNewFile();
FileChannel src = new FileInputStream(bdoriginal).getChannel();
FileChannel dst = new FileOutputStream(backup).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope this might be help you.
Upvotes: 0
Reputation: 628
you should also add following permission <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Upvotes: 4