Copy database file from asset












0














I'm using this code for copy database file from asset into /data/data/app/databases But I get this exception and apps crashes. What's problem?



Once worked!
I uninstall app each time I try my code!



 public static void copyFromAsset(Context context,String filename) throws IOException {

try{
// CHECK IS EXISTS OR NOT
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/"+context.getPackageName()+"/databases/"+filename,null, 0);
dbe.close();
}
catch(Exception e)
{
// COPY IF NOT EXISTS
AssetManager am = context.getAssets();
OutputStream os = new FileOutputStream("/data/data/"+context.getPackageName()+"/databases/"+filename);
byte b = new byte[1024];
int r;
InputStream is = am.open(filename);
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
Log.i("Writing", "Is processing...");
}
is.close();
os.close();
}

}




09-17 22:34:17.599: W/System.err(15674):    at libcore.io.Posix.open(Native Method)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.609: W/System.err(15674): ... 12 more
09-17 22:34:17.609: W/System.err(15674): java.io.FileNotFoundException: /data/data/ir.aiga.apps.quran/databases/database_surainfo: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:419)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.classes.FileOperations.copyFromAsset(FileOperations.java:106)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:736)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:1)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-17 22:34:17.619: W/System.err(15674): at java.lang.Thread.run(Thread.java:856)
09-17 22:34:17.619: W/System.err(15674): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.Posix.open(Native Method)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.619: W/System.err(15674): ... 12 more









share|improve this question
























  • Is the file in your asset folder of your application?
    – Droid Chris
    Sep 17 '14 at 18:28










  • 1.The folder databases is not created at instsll of your application. Check first if it exists and create before you try to put something in it. 2. A database file often has extension .db which you are not using. 2. You put code in a catch block. But that code should also have a try-catch.
    – greenapps
    Sep 17 '14 at 18:59
















0














I'm using this code for copy database file from asset into /data/data/app/databases But I get this exception and apps crashes. What's problem?



Once worked!
I uninstall app each time I try my code!



 public static void copyFromAsset(Context context,String filename) throws IOException {

try{
// CHECK IS EXISTS OR NOT
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/"+context.getPackageName()+"/databases/"+filename,null, 0);
dbe.close();
}
catch(Exception e)
{
// COPY IF NOT EXISTS
AssetManager am = context.getAssets();
OutputStream os = new FileOutputStream("/data/data/"+context.getPackageName()+"/databases/"+filename);
byte b = new byte[1024];
int r;
InputStream is = am.open(filename);
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
Log.i("Writing", "Is processing...");
}
is.close();
os.close();
}

}




09-17 22:34:17.599: W/System.err(15674):    at libcore.io.Posix.open(Native Method)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.609: W/System.err(15674): ... 12 more
09-17 22:34:17.609: W/System.err(15674): java.io.FileNotFoundException: /data/data/ir.aiga.apps.quran/databases/database_surainfo: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:419)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.classes.FileOperations.copyFromAsset(FileOperations.java:106)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:736)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:1)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-17 22:34:17.619: W/System.err(15674): at java.lang.Thread.run(Thread.java:856)
09-17 22:34:17.619: W/System.err(15674): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.Posix.open(Native Method)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.619: W/System.err(15674): ... 12 more









share|improve this question
























  • Is the file in your asset folder of your application?
    – Droid Chris
    Sep 17 '14 at 18:28










  • 1.The folder databases is not created at instsll of your application. Check first if it exists and create before you try to put something in it. 2. A database file often has extension .db which you are not using. 2. You put code in a catch block. But that code should also have a try-catch.
    – greenapps
    Sep 17 '14 at 18:59














0












0








0


1





I'm using this code for copy database file from asset into /data/data/app/databases But I get this exception and apps crashes. What's problem?



Once worked!
I uninstall app each time I try my code!



 public static void copyFromAsset(Context context,String filename) throws IOException {

try{
// CHECK IS EXISTS OR NOT
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/"+context.getPackageName()+"/databases/"+filename,null, 0);
dbe.close();
}
catch(Exception e)
{
// COPY IF NOT EXISTS
AssetManager am = context.getAssets();
OutputStream os = new FileOutputStream("/data/data/"+context.getPackageName()+"/databases/"+filename);
byte b = new byte[1024];
int r;
InputStream is = am.open(filename);
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
Log.i("Writing", "Is processing...");
}
is.close();
os.close();
}

}




09-17 22:34:17.599: W/System.err(15674):    at libcore.io.Posix.open(Native Method)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.609: W/System.err(15674): ... 12 more
09-17 22:34:17.609: W/System.err(15674): java.io.FileNotFoundException: /data/data/ir.aiga.apps.quran/databases/database_surainfo: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:419)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.classes.FileOperations.copyFromAsset(FileOperations.java:106)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:736)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:1)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-17 22:34:17.619: W/System.err(15674): at java.lang.Thread.run(Thread.java:856)
09-17 22:34:17.619: W/System.err(15674): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.Posix.open(Native Method)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.619: W/System.err(15674): ... 12 more









share|improve this question















I'm using this code for copy database file from asset into /data/data/app/databases But I get this exception and apps crashes. What's problem?



Once worked!
I uninstall app each time I try my code!



 public static void copyFromAsset(Context context,String filename) throws IOException {

try{
// CHECK IS EXISTS OR NOT
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/"+context.getPackageName()+"/databases/"+filename,null, 0);
dbe.close();
}
catch(Exception e)
{
// COPY IF NOT EXISTS
AssetManager am = context.getAssets();
OutputStream os = new FileOutputStream("/data/data/"+context.getPackageName()+"/databases/"+filename);
byte b = new byte[1024];
int r;
InputStream is = am.open(filename);
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
Log.i("Writing", "Is processing...");
}
is.close();
os.close();
}

}




09-17 22:34:17.599: W/System.err(15674):    at libcore.io.Posix.open(Native Method)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.609: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.609: W/System.err(15674): ... 12 more
09-17 22:34:17.609: W/System.err(15674): java.io.FileNotFoundException: /data/data/ir.aiga.apps.quran/databases/database_surainfo: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:419)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-17 22:34:17.619: W/System.err(15674): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.classes.FileOperations.copyFromAsset(FileOperations.java:106)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:736)
09-17 22:34:17.619: W/System.err(15674): at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:1)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-17 22:34:17.619: W/System.err(15674): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-17 22:34:17.619: W/System.err(15674): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-17 22:34:17.619: W/System.err(15674): at java.lang.Thread.run(Thread.java:856)
09-17 22:34:17.619: W/System.err(15674): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.Posix.open(Native Method)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.619: W/System.err(15674): at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.619: W/System.err(15674): ... 12 more






android sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:23









Cœur

17.3k9102143




17.3k9102143










asked Sep 17 '14 at 18:17









sdaasd

86




86












  • Is the file in your asset folder of your application?
    – Droid Chris
    Sep 17 '14 at 18:28










  • 1.The folder databases is not created at instsll of your application. Check first if it exists and create before you try to put something in it. 2. A database file often has extension .db which you are not using. 2. You put code in a catch block. But that code should also have a try-catch.
    – greenapps
    Sep 17 '14 at 18:59


















  • Is the file in your asset folder of your application?
    – Droid Chris
    Sep 17 '14 at 18:28










  • 1.The folder databases is not created at instsll of your application. Check first if it exists and create before you try to put something in it. 2. A database file often has extension .db which you are not using. 2. You put code in a catch block. But that code should also have a try-catch.
    – greenapps
    Sep 17 '14 at 18:59
















Is the file in your asset folder of your application?
– Droid Chris
Sep 17 '14 at 18:28




Is the file in your asset folder of your application?
– Droid Chris
Sep 17 '14 at 18:28












1.The folder databases is not created at instsll of your application. Check first if it exists and create before you try to put something in it. 2. A database file often has extension .db which you are not using. 2. You put code in a catch block. But that code should also have a try-catch.
– greenapps
Sep 17 '14 at 18:59




1.The folder databases is not created at instsll of your application. Check first if it exists and create before you try to put something in it. 2. A database file often has extension .db which you are not using. 2. You put code in a catch block. But that code should also have a try-catch.
– greenapps
Sep 17 '14 at 18:59

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25897637%2fcopy-database-file-from-asset%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25897637%2fcopy-database-file-from-asset%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()