Database is shown in emulator but not on my android device - android

Here is my code...
private static String DB_NAME = "pa1.db";
private static String DB_PATH = "/data/data/in.bitcode.sn/databases/";
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}

You cannot write to /data folder. This is an Android system folder. It works in the emulator because in the emulator you're root, while you're probably not in your device.
Maybe you want to use Android's integrated backup API instead of this.

Related

Opening a database from external storage Android

I was previously storing a sqlite database in my apps assets folder but have now moved the database to external storage.
My previous copyDatabase() method looked like this.
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
OutputStream myOutput = new FileOutputStream(DB_PATH);
byte[] buffer = new byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT];
while (true) {
int length = myInput.read(buffer);
if (length > 0) {
myOutput.write(buffer, 0, length);
} else {
myOutput.flush();
myOutput.close();
myInput.close();
return;
}
}
}
The issue is I'm unsure how to create an InputStream for opening the database from external storage. I can't seem to find the external storage equivalent to myContext.getAssets().open(DATABASE_NAME);
The current database path:
DB_PATH = Environment.getExternalStorageDirectory().getPath().toString()+"/SoulInfoDatabase/BB2SoulDatabase.db";
Step 1: Give storage permission in your App Manifesto file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Step 2: Copy database to your custom SD card Path
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + "/" + DB_NAME;
// Open the empty db as the output stream
new File(outFileName).createNewFile();
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
Step 3: Then Open your database:
try {
db = SQLiteDatabase.openDatabase(DB_PATH + "/" + DB_NAME, null,
SQLiteDatabase.OPEN_READWRITE
| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
WHERE
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SoulInfoDatabase";
File file = new File(filePath);
if(!file.exists()){
file.mkdirs();
}
DB_PATH = filePath;
DB_NAME = "BB2SoulDatabase.sqlite";

Loading database from asset folder - Fileoutputstream fails

My problem is that my application always fails when the database is copied from asset folder to the phone path:
/data/data/at.atn.android/databases/
MY databasename:
atnRoutenplaner.sqlite3
My code for the transfer:
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
File sampleFile = new File(outFileName);
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
try this,
public void CopyDataBaseFromAsset() throws IOException{
InputStream in = ctx.getAssets().open("mycontacts");
Log.e("sample", "Starting copying" );
String outputFileName = DATABASE_PATH+DATABASE_NAME;
File databaseFile = new File( "/data/data/com.copy.copydatabasefromasset/databases");
// check if databases folder exists, if not create one and its subfolders
if (!databaseFile.exists()){
databaseFile.mkdir();
}
OutputStream out = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer))>0){
out.write(buffer,0,length);
}
Log.e("sample", "Completed" );
out.flush();
out.close();
in.close();
}

how to solve a databse issue in actual android device?

I have created database in my application and stored data in that database now when i run my app in emulater it shows all the data but when i run the same app in my android device it shows the error ?
here is my code.
private static String DB_NAME = "pa1.db";
private static String DB_PATH = "/data/data/in.bitcode.sn/databases/";
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
you should not set the database path fixed but use
context.getDatabasePath(DB_NAME)

Copying database form assets fails on emulator

I used this tutorial to include a database file to my android app. It works fine on my HTC Decire HD. I wanted to run it on emulator to see if tablet layouts look well. Unfortunately the app fails with an error.
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){ <------ HERE, at first iteration
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
The message for this error is just 'null', nothing more. Can this be fixed?
private void copyfromAsset()
{
try {
String FILE_TO_READ="data.txt"; //file in asset folder
String TEMP_FILE_NAME="temp.txt"; //or whatever file name you want to give
byte[] buffer = new byte[1024];
int len1 = 0;
InputStream istr=(con.getAssets().open(FILE_TO_READ));
FileOutputStream fos=openFileOutput(TEMP_FILE_NAME,MODE_WORLD_READABLE);
while ((len1 = istr.read(buffer)) !=-1) {
fos.write(buffer, 0, len1); // Write In FileOutputStream.
}
fos.flush();
fos.close();
istr.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
try this method it is working fine for me....hit accept if you found usefull..
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
}
else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are going to be able to overwrite that
// database with our database.
try {
copyDataBase();
}
catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* #return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transferring byte-stream.
*/
private void copyDataBase() throws IOException {
// Open your local DB as the input stream
InputStream myInput = mContext.getAssets().open(DB_NAME);
// Path to the just created empty DB
String outFileName = DB_PATH + DB_NAME;
// Open the empty DB as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the input-file to the output-file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}

Convert Android Code to MonoAndroid Code

How do I convert the following Android code to MonoAndroid code?
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
And are there any special dll references or using statements?
Thanks
After days of fiddling around and futile googling, 'Assets.Open(#"test.db")' was the key:
//Open your local db as the input stream
Stream myInput = Assets.Open(#"test.db");
string outFileName = Path.Combine(System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), "test.db");
//Open the empty db as the output stream
Stream myOutput = new FileStream(outFileName,FileMode.OpenOrCreate);
byte[] buffer = new byte[1024];
int b = buffer.Length;
int length;
while ((length = myInput.Read(buffer,0,b))>0){
myOutput.Write(buffer, 0, length);
}
//Close the streams
myOutput.Flush();
myOutput.Close();
myInput.Close();
}

Categories

Resources