import external db into app and use it in android - java

Im new to android development. Now im trying to use sqlite db.
I created a database sqlite file using sqlite manager.
I imported it to the project by /data/data/packagename/dbname, it works fine in emulator , but if I took release in device the app crashes,I didnt know what happens and why it happens. Any Suggestions for it?

You cannot use a External DB in that manner. When you import it into your project it doesn't mean it is available in all devices from there after(Assuming you used DDMS for this). It means that DB is available to that particular emulator only. Follow the below link to find out how to add a External DB to your Application..
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

private void StoreDatabase() {
File DbFile=new File("data/data/packagename/DBName.sqlite");
if(DbFile.exists())
{
System.out.println("file already exist ,No need to Create");
}
else
{
try
{
DbFile.createNewFile();
System.out.println("File Created successfully");
InputStream is = this.getAssets().open("DBName.sqlite");
FileOutputStream fos = new FileOutputStream(DbFile);
byte[] buffer = new byte[1024];
int length=0;
while ((length = is.read(buffer))>0)
{
fos.write(buffer, 0, length);
}
System.out.println("File succesfully placed on sdcard");
//Close the streams
fos.flush();
fos.close();
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

View this link, this will be very helpful if you are using your own database in Android.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
in this tutorial you have to put your database in assets folder of your project and the database will automatically transferred to database folder of you device.

See https://github.com/jgilfelt/android-sqlite-asset-helper for a helper lib to take care of this.
(I haven't used this library personally but I came across it yesterday while searching for something else. It appears to do what you need, though).

Related

Persist Map <Integer, Integer> in android

I tried this weekend with different approaches to store data of a map <Integer, Integer> in android. Unfortunately, this has not worked as desired.
Corresponding code section:
if (getCounter() > 19){
try
{
File folder = new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "hashmap");
if (!folder.exists())
{
folder.mkdirs();
}
File ganiuSave = new File(folder, "mapDEGR");
OutputStream mapSave = new FileOutputStream(ganiuSave);
ObjectOutputStream oos = new ObjectOutputStream(mapSave);
oos.writeObject(mapDEGR);
oos.close();
mapSave.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}}
I could not locate the corresponding file on the smartphone. The app was previously transferred to the smartphone using android debug bridge. The map contains values, so this should not be a reason why no saving takes place. All classes involved implement Serializable and no exception got thrown.
Anyone any ideas?
Tell me if you need more surrounding code to evaluate the issue. Code segment is located inside a switch statement.
To locate your file you can log your file's absolute location like this:
Log.v(LOG_TAG, "File Location: " + ganiuSave.getAbsolutePath());
Once you get the exact location, you can reach the file by using ADB or Android Studio's "Device File Explorer". When I tried with your code I'm getting:
On the emulator api 17;
/mnt/sdcard/Android/data/com.example.yourapp/files/Download/hashmap/mapDEGR
on a physical device api 24;
/storage/emulated/0/Android/data/com.example.yourapp/files/Download/hashmap/mapDEGR

Keeping saved files of app after an update

I've seen this question, it's not a duplicate.
I'm pretty new to Android-Development, so my way to store personal data on the device might be not the best one.
However, when I update the App, the stored files aren't found anymore.
File path = myContext.getFilesDir();
File pathUse = new File(path, "lists.ser");
if(!pathUse.exists()) {
try {
pathUse.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try(ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(pathUse))) {
os.writeObject(myObject);
} catch(Exception e) {
e.printStackTrace();
}
How can I store the data that way, that it is kept after an update?
Internal files are removed only if you uninstall and reinstall. An upgrade will not remove internal files -- and it shouldn't. Consider that if it did that, your database would be removed for every upgrade as well.
Check the official documentation for more Info Click here
Hence storing the application specific data in external storage is not recommended because anyone can access it.

Android file is hidden on external storage

I am writing out a file to the external storage on android. The file shows up in my device when I browse to location using the ap ES file explorer.
But when I plug my device in to windows, the file does not show up.
Furthermore if I write an empty file named "test.wav" to Ringtones folder, no test.wav will show up in my ringtones settings.
But, if I create an empty file in windows named "test.wav", and drag and drop into my Ringtones folder, it will then properly show up in my ringtones browser in settings.
Code I am using is a follows.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_RINGTONES), "testfile.wav");
file.setReadable(true);
try {
FileOutputStream fileOut = new FileOutputStream(file);
stream = new DataOutputStream(fileOut);
stream.flush();
stream.close();
//this code successfully creates a file
// but file is not viewable by all means
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Android uses tables to keep track of images, videos, sounds and so on these tables called " content providers " for example it uses MediaStore.Images.Media.DATA for images Note that these tables are used for the view this illustrates why you can only see it using ES. When you add new file manually, you need to add an entry for this file in the corresponding table or refresh your music library in case of sound file was added, but when you add file using windows the device's driver updates its tables automatically. Try refreshing your music application or even restarting your phone if this solves the problem updating your content provider is the solution.
I could be wrong but I think that it is /extsd showing when you plug your device into Windows, whereas external storage is actually /sdcard. so your file is in /sdcard, not in /extsd, that's why you don't see it. At least whenever I plugged Android tab into Windows, I could only browse its removable SD, i.e. /extsd but I couldn't see /sdcard

.sql file in eclipse, show it as a table?

In my app I want a small database (.sql), with data about energy (kCal) in food.
The problem is, I have a bit expertise with SQL, but I don't really get my database into my app. Do I have to make a new .java file? Or I've read somewhere else, in my assets? I think the database isn't on a server, just within the app. The project I already have, is an about about food, overweight and BMI. Now I want to add a database about the amount of kCal in food.
I do not really know what I have to say more here. Hopefully you understand what I'm trying to do. I've read ALL related questions here on Stackoverflow, but still didn't got an answer.
If you can help me, you would be my hero! :)
Thank you all in advance,
Jacob
Android's built-in on-device DBMS is not mysql, but SQLite. You can find an excellent tutorial on setting it up and integrating it into your app here.
Create the SQLite database file with whatever data you want in it on your PC. Then create an assets folder in your project root in Eclipse (same level as src/, bin/, libs/, res/, etc.). Put your SQLite file in there. Then write some code to copy that asset file into the correct location on the device. Following is the code I currently use for that. I wrote it a while back when I was a beginner so there is probably a better way, but this works.
try {
InputStream is = getActivity().getApplicationContext().getAssets().open("sourceDatabaseFile.db");
OutputStream os = new FileOutputStream(getActivity().getApplicationContext().getDatabasePath("destinationDatabaseFile.db"));
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
Another way would be to create the database using some Android code, and then have a (giant) class containing all the SQL statements to insert the data into the database.

Unable to write to the Internal Storage

Please have a look at the following code
File folder = new File("/Main Note/Sub Notes/"+dateStr+"/");
File file = new File(folder+name.getText().toString()+".txt");
try
{
if(!folder.exists())
{
folder.mkdirs();
}
FileOutputStream outputStream = openFileOutput(file.getName(),Context.MODE_WORLD_WRITEABLE);
outputStream.write(spokenText.getBytes());
outputStream.flush();
outputStream.close();
Toast.makeText(VoiceNotes.this, "Data Successfully written to: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
}
catch(IOException io)
{
Toast.makeText(VoiceNotes.this, "Error in Writing to SD", Toast.LENGTH_LONG).show();
}
Here I am trying to write data to the internal memory. This has no errors, displays the data has been written successfully.
But when I navigate to the Internal SD in phone, I don't see any folder or file it created! I guess I have done something wrong, this is the first time I am writing to the internal storage in Android.
File folder = new File(context.getFilesDir(),"/MyFolder/");
files will be created in "/data/data/app.package/files/...". But you can see them only if device was rooted
You want to access the path returned by getExternalStorageDirectory() as described here.

Categories