I am trying this code:
By using this code in my example, I am always getting a FileNotFoundException
, though the folder exists in external storage while reading FileInputStream.
Here is my code:
public void exportdata()
{
try
{
myInput = new FileInputStream("/data/data/com.example.hello/databases/BikeMaintenance.db");
File directory = new File("/sdcard/BikeMaintenance/Data/");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
OutputStream myOutput = new FileOutputStream(directory.getPath()+"/BikeMaintenance.backup");
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
msg="Backup Succesfull!";
Toast.makeText(MainPage.this,msg,Toast.LENGTH_SHORT).show();
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
catch (FileNotFoundException e)
{
msg="FileNotFoundException";
Toast.makeText(MainPage.this,msg,Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
catch (IOException e)
{
msg="Backup Unsuccesfull!";
Toast.makeText(MainPage.this,msg,Toast.LENGTH_SHORT).show();
}
}
Where am I going wrong? What may be the reason for this? I searched solution for this, but didn't find a solution.
Here is the logcat output:
java.io.FileNotFoundException: /data/data/com.example.hello/databases/BikeMaintenance.db: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:416) at java.io.FileInputStream.(FileInputStream.java:78) at java.io.FileInputStream.(FileInputStream.java:105) at com.example.hello.MainPage.exportdata(MainPage.java:625) at com.example.hello.MainPage.backup(MainPage.java:692) at com.example.hello.MainPage$1.onClick(MainPage.java:494) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) at libcore.io.IoBridge.open(IoBridge.java:400) ... 14 more
Best Answer
your code looks good to me but What i think is you dont have permission to access that .db file you can use DataBaseHelper class to access your .db file