Saving OSM map tiles in app project - android

Just wondering is it possible to add the saved map tiles from the mobile atlas to the project instead of sd card?
i want the map tiles to be available within the app i will be releasing. It is a free app on android market.
The map tiles are divided into 4 zip files, is it possible to keep and use them from the project file or do i need an online server to download them from.
Any help would be great
Thank you

One options is to use the assets folder to store files and then copy them to your private app data directory. Something like this should work. This copies a database, but you could adapt it to copy your zip files.
/**
* 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 transfering
* bytestream.
* */
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.toString();
// boolean success = DB_PATH.mkdirs();
// 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();
}

Related

Android studio copy database file to internal storage

Is there anyway to copy Database file to internal storage instead of external by pressing a Button?
define DB_PATH and DB_NAME before calling this method.
/**
* *******************************************
* 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 bytestream.
* *******************************************
*/
private void copyDataBase() throws IOException {
String DB_NAME = "AnArbitraryName.db";
String DB_PATH = myContext.getDatabasePath(DB.NAME).getPath();
// 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;
// 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();
Static.getSharedPreference(myContext).edit()
.putInt("DB_VERSION", Utils.Version.GetVersion()).commit();
}
Yeah you can just save the db object to internal storage by getting a FileOutputStream to internal storage by calling context.openFileOutput(outputFile, Context.MODE_PRIVATE);.
More info here.

Not able to access sqlite database Android

Ive created an app that stores user login information into an sqlite database. However when i try to access it using Root Explorer i get an error.
An error occurred while opening the database. disk i/o error (code 3850):, while compiling: Select * from sqlite_master where type in('table','view') order by name.
The app works fine, and it is storing the log in information as required. However i cant access it on the root explorer?
Any suggestions?
please call this function:
public static void copyDataBase(Context mActivity) throws IOException {
InputStream myInput = new FileInputStream(new File("/data/data/"
+ mActivity.getPackageName() + "/databases/" + "yourdb.sqlite"));
File files = new File("/sdcard/files/");
files.mkdirs();
String outFileName = "/sdcard/files/your.sqlite";
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int bufferLength;
while ((bufferLength = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, bufferLength);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
your database is copied /sdcard/files/your.sqlite this path.
You could add a button in your app to view the database information or write the database information to a file in external storage.
Otherwise, maybe you need to root your device to access the database.
Just give permissions to all users to the file [YOUR_DB].db-journal
For me it worked.

How to put .db file from assets to data/data/packagename/ android [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to use an existing database with an Android application
how to put .db file from assets to data/data/packagename/ without using copying content of .db file in assets. I don't want to create database because its useless to put the .db file in assets. I explore on it but all are again creating databse but i only want to put that .db file directly.
use this
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 = "/data/data/"
+getApplicationContext().getPackageName()
+ "/databases/" + 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();
}

Not empty LiteSQL DB at start

I think that this is rather easy question. I am too young in android stuff already. I want to prepare application which will be using database. In every example I've shown, there is an empty database where application is firstly started and after that there are some inserts. I want to have app with rather big db so I want to have filled db when app is started. How can I prepare db and attach it to program?
put your filled database in Package's Asset directory,
at application runtime just copy that database to application's internal storage like
data/data/<package name>/database directory.
then use it.
EDIT: this for copy database from asset directory to database directory,
private void copyDataBase() throws IOException {
try {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open("your Database file name");
// Path to the just created empty db
String outFileName = "/data/data/<package name>/databases/";
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();
} catch (Exception e) {
Log.e("error", e.toString());
}
}

problem in copying data from assets to application

For my application i need to read data from my own data base. I put my db in "ASSETS" folder and using following code to copy into the path
DB_PATH = "/data/data/com.android.example/databases/"
But the table is not getting created.And its throwing exception and forced close.
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){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
In above code "buffer" is not getting data . so its not writing data to myOutput. I found this code in net and modified . could any body help me to read the file in assets folder to SD card.I need to store data in SD card from external file and read from it.
Check you database file size, there is a limitation around 1.2 Mb for files in the "assets" folder.
One way around it is to use the Unix "split" command, add the split files in the res/raw folder, and then splice them back together into the db by altering the code you have slightly.
Read this blog (http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/) for more information.
You must put data not in ASSETS folder but into assets - please double check!

Categories

Resources