Appropriate storage and display of my bookmarks/history activity? - android

I want a simple bookmarks and/or history for my app, and I'm wondering what the most appropriate storage would be? A text in a text file or preference, or perhaps a database? Which would be most flexible across updates, and efficient for space and lookup time?
For the display, I'm thinking this would be a good starting point, but would it be easy to add an icon to some items?
Edit:
I finally set up a Bookmark activity that should connect to a database:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarkview);
Cursor cursor = managedQuery(getIntent().getData(), new String[] {Bookmark.TITLE, Bookmark.URL},
null, null, Bookmark.DEFAULT_SORT_ORDER);
setListAdapter(new SimpleCursorAdapter(this, R.layout.bookmarkitem, cursor,
new String[] { Bookmark.TITLE }, new int[] { android.R.id.text1 }));
findViewById(R.id.addBookmark).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put("url", _url);
values.put("title", _title);
// When the update completes,
// the content provider will notify the cursor of the change, which will
// cause the UI to be updated.
getContentResolver().update(_myuri, values, null, null);
}
});
}
Bookmark.java:
package com.tunes.viewer.Bookmarks;
import android.net.Uri;
import android.provider.BaseColumns;
/*
* Database will have:
* pk - primary key
* title - the name of the bookmark.
* url - the url.
*/
public class Bookmark implements BaseColumns{
public static final String AUTHORITY = "com.tunes.viewer";
/**
* The content:// style URL for this table
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/Bookmarks");
/**
* The MIME type of {#link #CONTENT_URI} providing a directory of notes.
*/
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.note";
/**
* The MIME type of a {#link #CONTENT_URI} sub-directory of a single note.
*/
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.note";
/**
* The default sort order for this table
*/
public static final String DEFAULT_SORT_ORDER = "title";
/**
* The title of the note
* <P>Type: TEXT</P>
*/
public static final String TITLE = "title";
/**
* The url
* <P>Type: TEXT</P>
*/
public static final String URL = "url";
}
I seem to have fixed most of the problems I was having, but unfortunately it doesn't add to the database when I click the Add button (calling the onclick above). Furthermore, I added data to the database, but it doesn't show up in the view. What's wrong with the cursor/adapter here? Full source is here.

i would suggest, you go with database. It will be easy and efficient solution for your requirement.
A single table in sqlite will suffice to your requirements. as you will need to maintain a list of url you visited. this table will also serve your requirement of storing bookmark.
your table format could be something like this.
_____________________________________________________________________________________
Id(Auto-increment) | Title of page | Url of Page |name of icon(if needed) |isBookmark |
_____________________________________________________________________________________
This could be a good structure to achieve you requirement. set isBookmark to 0/1 to set specific link as bookmark or unbookmark it.
EDIT
I did not suggest you to use SharedPreferences and i wont (though it is straight forword and easy to implement) and reason lies in very definition of SharedPreferences which says:
"The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings."
Now i can not imagine a single way to store ArrayList<String>(Urls) in one of these primitive datatypes.
There is one more work around.and it is Object Serialization. you can save your complete arraylist instance to a file and next time when you need this object, deseralize it similarly.. Here is the sample code for Serialization.
.
public void serializeMap(ArrayList<String> list) {
try {
FileOutputStream fStream = openFileOutput(namefile.bin, Context.MODE_PRIVATE) ;
ObjectOutputStream oStream = new ObjectOutputStream(fStream);
oStream.writeObject(list);
oStream.flush();
oStream.close();
Log.v("Serialization success", "Success");
} catch (Exception e) {
Log.v("IO Exception", e.getMessage());
}
}
But this approach is not much recommended though.

I agree with WareNinja- the SharedPreferences data areas would be sufficient for simple, non-relational data. This SO answer is very comprehensive in describing the nuances of implementing Activity and/or application-wide SharedPreferences.
The SharedPreferences framework will take care of all the data persistence minimising the amount of code you will need to write and your exposure to db-like 'update' transactions.
However be aware, in terms of expanding your application this medium is powerful but inflexible. The minute you feel the need to expand the type of data being stored move to SQLite or similar.

I suggest using SharedPreferences and/or memdiskcache, both works fast and seamlessly.
p.s. nothing against using sqlite, however i always try to avoid using db for client apps.
example lib to abstract local storage (no db!);
https://github.com/wareninja/generic-store-for-android
you can store and retrieve any type of data in key-value form,
sample code for loading java object from memdiskcache;
String keyPrefix = "blabla1";
if (GenericStore.isCustomKeyExist(GenericStore.TYPE_MEMDISKCACHE, keyPrefix, this)) {
mNewPoiDataList = (NewPoiDataList)GenericStore.getObject(GenericStore.TYPE_MEMDISKCACHE
, keyPrefix, this);
}

Related

How to store video content in SQLite database (not the video path)

I want to store a video in sqlite database. P.S. I do not want to store the path but the actual video contents.
I have converted the video in byte array and stored byte array in sqlite database. Upon retrieval bytearray is being converted into File. But video is not playing. Please help.
I want to store a video in sqlite database. P.S. I do not want to
store the path but the actual video contents.
Unless the videos are very short and take up little space (say up to 200k each, perhaps 1/10th of a second but would depend upon the format it is saved in) then you would likely encounter issues and exceptions/crashes.
Using a phone around 2 seconds of black took up 2.2Mb, 2 seconds of actually recording a video took up 7Mb.
Although SQLite has the ability to store relative large BLOB's as per :-
Maximum length of a string or BLOB
The maximum number of bytes in a string or BLOB in SQLite is defined
by the preprocessor macro SQLITE_MAX_LENGTH. The default value of this
macro is 1 billion (1 thousand million or 1,000,000,000). You can
raise or lower this value at compile-time using a command-line option
like this:
-DSQLITE_MAX_LENGTH=123456789 The current implementation will only support a string or BLOB length up to 231-1 or 2147483647. And some
built-in functions such as hex() might fail well before that point. In
security-sensitive applications it is best not to try to increase the
maximum string and blob length. In fact, you might do well to lower
the maximum string and blob length to something more in the range of a
few million if that is possible.
During part of SQLite's INSERT and SELECT processing, the complete
content of each row in the database is encoded as a single BLOB. So
the SQLITE_MAX_LENGTH parameter also determines the maximum number of
bytes in a row.
The maximum string or BLOB length can be lowered at run-time using the
sqlite3_limit(db,SQLITE_LIMIT_LENGTH,size) interface.
Limits In SQLite
The Android SDK's CursorWindow has a limitation of 2Mb and that is for all the columns of the row(s) if buffers. As such even if you can store Videos successfully, you may not be able to retrieve those Videos.
The recommended way is what you don't want, that is to store the path to the Video.
If i store the video in my internal/external storage and store the
path instead then how will i be able to access the same from some
other device.
You would have the same issue with the database as it's typically stored within the Applications data which is protected. That is unless the database is a pre-existing database (i.e. populated with data), in which case the database is distributed with the App via the APK.
If the latter, a pre-existing database distributed via the APK, then the videos can also be distributed as part of the APK and hence as protected as and as exposable as the database.
If your intention is to distribute videos between devices that are not part of the APK then SQlite is probably not the correct solution as it's an embedded database and has no client/server functionality built in.
Besides what if my device gets formatted then I will lose all the
data.
In such a scenario, the database would be as vulnerable as any other data, as that is all the database is, a file, just like a video, a word document etc which all need a suitable application to view/change the content. However, if the database is a pre-existing database, then simply re-installing the App would restore the database and other files from the APK.
Working Example
This uses the Suggested/Recommended method assuming the videos are to be distributed with the APK.
Note Videos Courtesey of Sample Videos
After creating new project 4 videos were downloaded and copied into the res/raw folder (after creating the raw folder) as per :-
The Database Helper (subclass of SQLiteOpenHelper) was created for a 2 column table an with
- _id column (note named _id for use with SimpleCursorAdapter).
- video_path for storing the path/name of the video (not the full path but sufficient to be able to determine the path from the data stored)
- Note UNIQUE has been coded to stop duplicates being added.
With some basic method to allow rows to be added and deleted and for all rows to be extracted (via a Cursor for use with the SimpleCursorAdapter).
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "myvideos";
public static final int DBVERSION = 1;
public static final String TBL_VIDEO = "video";
public static final String COL_VIDEO_ID = BaseColumns._ID;
public static final String COL_VIDEO_PATH = "video_path";
SQLiteDatabase mDB;
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
String crt_video_table = "CREATE TABLE IF NOT EXISTS " + TBL_VIDEO + "(" +
COL_VIDEO_ID + " INTEGER PRIMARY KEY," +
COL_VIDEO_PATH + " TEXT UNIQUE" +
")";
db.execSQL(crt_video_table);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long addVideo(String path) {
ContentValues cv = new ContentValues();
cv.put(COL_VIDEO_PATH,path);
return mDB.insert(TBL_VIDEO,null,cv);
}
public Cursor getVideos() {
return mDB.query(TBL_VIDEO,null,null,null,null,null,null);
}
public int deleteVideoFromDB(long id) {
String whereclause = COL_VIDEO_ID + "=?";
String[] whereargs = new String[]{String.valueOf(id)};
return mDB.delete(TBL_VIDEO,whereclause,whereargs);
}
}
A pretty straigforward MainActivity.java (see comments)
public class MainActivity extends AppCompatActivity {
TextView mMyTextView;
ListView mVideoList;
VideoView mVideoViewer;
DBHelper mDBHlpr;
Cursor mCsr;
SimpleCursorAdapter mSCA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMyTextView = this.findViewById(R.id.mytext);
mVideoList = this.findViewById(R.id.videolist);
mVideoViewer = this.findViewById(R.id.videoviewer);
mDBHlpr = new DBHelper(this);
addVideosFromRawResourceToDB();
}
#Override
protected void onDestroy() {
mCsr.close(); //<<<<<<<<<< clear up the Cursor
super.onDestroy();
}
#Override
protected void onResume() {
super.onResume();
manageListView(); //<<<<<<<<<< rebuild and redisplay the List of Videos (in case they have changed)
}
/**
* Setup or Refresh the ListView adding the OnItemClick and OnItemLongClick listeners
*/
private void manageListView() {
mCsr = mDBHlpr.getVideos();
// Not setup so set it up
if (mSCA == null) {
// Instantiate the SimpleCursorAdapter
mSCA = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1, // Use stock layout
mCsr, // The Cursor with the list of videos
new String[]{DBHelper.COL_VIDEO_PATH}, // the column (columns)
new int[]{android.R.id.text1}, // the view id(s) into which the column(s) data will be placed
0
);
mVideoList.setAdapter(mSCA); // Set the adpater for the ListView
/**
* Add The Long Click Listener (will delete the video row from the DB (NOT the video))
*/
mVideoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
mDBHlpr.deleteVideoFromDB(id);
manageListView(); // <<<<<<<<<< refresh the ListView as data has changed
return true;
}
});
/**
* Play the respective video when the item is clicked
* Note Cursor should be at the correct position so data can be extracted directly from the Cursor
*/
mVideoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setCurrentVideo(mCsr.getString(mCsr.getColumnIndex(DBHelper.COL_VIDEO_PATH)));
}
});
} else {
mSCA.swapCursor(mCsr); //<<<<<<<<<< apply the changed Cursor
}
}
/**
* Set the currrent video and play it
* #param path the path (resource name of the video)
*/
private void setCurrentVideo(String path) {
mVideoViewer.setVideoURI(
Uri.parse(
"android.resource://" + getPackageName() + "/" + String.valueOf(
getResources().getIdentifier(
path,
"raw",
getPackageName())
)
)
);
mVideoViewer.start();
}
/**
* Look at all the resources in the res/raw folder and add the to the DB (not if they are duplicates due to UNQIUE)
*/
private void addVideosFromRawResourceToDB() {
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
mDBHlpr.addVideo(fields[count].getName());
}
}
}
Results
When first started (nothing plays) :-
After long clicking the 1Mb video (deleting the DB entry) :-
After clicking A Video in the List :-
You can use this approach
When save the video, save it in app private storage folder.
Context.getFilesDir()
This will give you the path to the app storage in ..\Andorid\data\data\com.example.app
and it will be in internal storage.
Where com.example.app will be your application package id. You can make a new folder here like Videos then save videos in this folder. Save its path in the DB. Only your app can access to this folder. No any other app or device user can access this folder. So no one can edit or delete your files except your application.
Moreover if user reset mobile this data will be deleted as well as your database and maybe your app too in some cases. So no need to worry about it that your files will be deleted but database has still their path. If file deleted then DB deleted too as well but only when app Uninstall, device reset or SD card erase.

Write Parcelable into ContentProvider?

I have a really really huge Object which is Parcelable. It's so big that i dont event want to think of putting all those values into my ContentProvider manually. So since my Object already is Parcelable:
Is there any way of just writing the whole parcelable object into my ContentProvider?
Is there any way of just writing the whole parcelable object into my ContentProvider?
No, for the simple reason that Parcelable is not meant for storage. It is only for transferring data across process boundaries.
I found a solution for that! Use the Google Gson to convert the whole object to a json string:
/**
*
* #param context
* #param email
* #param customer
*/
public static void insertCustomer(Context context, String email, Customer customer) {
String json = new Gson().toJson(customer);
ContentValues values = new ContentValues();
values.put(DatabaseAdapterCustomer.COLUMN_CUSTOMER_EMAIL, email);
values.put(DatabaseAdapterCustomer.COLUMN_CUSTOMER_DATA, json);
final ContentResolver resolver = context.getContentResolver();
resolver.insert(ContentProviderCustomer.CONTENT_URI, values);
}
If you select it back you simply need to do:
String json = cursor.getString(cursor.getColumnIndex(DatabaseAdapterCustomer.COLUMN_CUSTOMER_DATA));
return new Gson().fromJson(json, Customer.class);

How to load database data, and store it into array list, and show it into ListVIew

So, I want to build an app to view some data from database. I already have the database, also already made some entities that have exactly same properties name with the column names in database. And also I put the database into database directory by copying from assets folder.
What I want to achieve is, I want to pull some data, and put it into array list, so I can show it in ListView in fragment.
Is there any convenient way to pull some data without querying (like loadAll() function) ?
For now, I'm using cursor to save the pulled data using query, and assign its properties one by one using set function like setName(String name).
After that, I show the list using CursorAdapter.
It would be like this
public class FrameCursor extends CursorWrapper{
/**
* Creates a cursor wrapper.
*
* #param cursor The underlying cursor to wrap.
*/
public FrameCursor(Cursor cursor) {
super(cursor);
}
public ZFrame getFrame(){
if(isBeforeFirst() || isAfterLast()){
return null;
}
ZFrame frame = new ZFrame();
ZFrameDao frameDao = new ZFrameDao();
int frameEdition = getInt(getColumnIndex(COLUMN_FRAME_EDITION));
int frameId = getInt(getColumnIndex(COLUMN_FRAME_ID));
int frameNumber = getInt(getColumnIndex(COLUMN_FRAME_NUMBER));
int frameType = getInt(getColumnIndex(COLUMN_FRAME_TYPE));
int frameBookmark = getInt(getColumnIndex(COLUMN_FRAME_BOOKMARK));
int frameGlyph = getInt(getColumnIndex(COLUMN_FRAME_GLYPH));
int frameLesson = getInt(getColumnIndex(COLUMN_FRAME_LESSON));
String frameAllReading = getString(getColumnIndex(COLUMN_FRAME_ALL_READING));
String frameReadingNumber = getString(getColumnIndex(COLUMN_FRAME_READING_NUMBER));
String frameReference = getString(getColumnIndex(COLUMN_FRAME_REFERENCE));
String frameWritingNumber = getString(getColumnIndex(COLUMN_FRAME_WRITING_NUMBER));
frame.setZEDITION(frameEdition);
frame.setZFRAME_ID(frameId);
frame.setZFRAME_NUMBER(frameNumber);
frame.setZFRAME_TYPE(frameType);
frame.setZBOOKMARK(frameBookmark);
frame.setZGLYPH((long)frameGlyph);
frame.setZLESSON((long)frameLesson);
frame.setZALL_READING_NUMBER(frameAllReading);
frame.setZREADING_NUMBER(frameReadingNumber);
frame.setZREFERENCE(frameReference);
frame.setZWRITING_NUMBER(frameWritingNumber);
return frame;
}
}
It would be consume lot of work for doing this for every table.
So anyone could help me?
Why not using CursorLoader ? Use CursorLoader to handle the cursor query issue, and it works perfectly with CursorAdapter, Here is the google's guide
Maybe this is what you need, a light weight orm api? You can go here for more information.

How can I save user-created objects to storage in android?

In my app, I have flashcard objects that the user creates themselves. Users can create as many flashcards as they want, but when they exit the app and return they need to be able to see the flashcards that they previously created and be able to delete them. I have it set up so that they can create/delete, but if they exit the app they will all delete automatically. What is the best way to save the information for a flashcard? It has at least 3 strings currently, the title, the front and the back.
I looked at a few, but am not sure how I would include all three strings in the saving options that are on the android developer site.
For example shared preferences, looks like you can only save certain settings, but it allows the user to change those settings.
The internal/external storage, although very different throw up the same problem, how to have unlimited number of objects and especially how to save all three strings separately.
This is the internal storage is shown below.
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
I don't see how you can save multiple number of objects or 3 different strings.
Does anyone see a solution to my problem?
SharedPreferences seem like the simplest way for you to achieve it, and I think you've misunderstood their usage, or confused the name with a 'Preferences' screen, as you can use the SharedPreferences methods to save anything (well, any basic datatype) persistently.
For example, I use it to save my app's JSON data (which might be a decent way for you to go in terms of saving you users' flashcards in a JSONArray).
/**
* Retrieves data from sharedpreferences
* #param c the application context
* #param pref the preference to be retrieved
* #return the stored JSON-formatted String containing the data
*/
public static String getStoredJSONData(Context c, String pref) {
if (c != null) {
SharedPreferences sPrefs = c.getSharedPreferences("AppPreferences", Context.MODE_PRIVATE);
return sPrefs.getString(pref, null);
}
return null;
}
/**
* Stores the most recent data into sharedpreferences
* #param c the application context
* #param pref the preference to be stored
* #param policyData the data to be stored
*/
public static void setStoredJSONData(Context c, String pref, String policyData) {
if (c != null) {
SharedPreferences sPrefs = c.getSharedPreferences("AppPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sPrefs.edit();
editor.putString(pref, policyData);
editor.commit();
}
}
Where the string 'pref' is a tag used to refer to that specific piece of data, so for example: "taylor.matt.data1" would refer to a piece of data and could be used to retrieve or store it from SharedPreferences

Storing an arraylist for retrieval in Android

I'm currently working on an Android application that is going to be somewhat like a student planner. My backend is all java and I am currently stuck because i am storing the objects that i create from my backend to arraylist. Being java, these objects dissapear as soon as the programs terminate. Whats the easiest way that i can store my java objects for retrieval the next time i boot my application? Any help is much appreciated! Im developing on 2.3 and eclipse(juno).
One of the data storage options listed in the Android developer tutorial will be the easiest thing to do. Which is the best fit will depend on how much data you're storing and how you need to access it. As the page says, the SharedPreferences class is best for a handful of items; for larger data sets, you could use Java serialization or some other way to write them to a file on the phone's storage; and if your data are large and/or you need structured access to them, an SQLite database is your best bet.
You can use Shared Preferences to store your data. The SharedPreferences lets you persist key-value pairs of primitive data types in you application. You can't store an entire ArrayList with a single key, but you can iterate over the array and systematically generate a key for each value in the list. I usually do something like this:
public class SomeActivity extends Activity {
private ArrayList<Data> list; //We'll persist this array
/* snip */
//These strings can be anything - you just need something you can use to systematically generate distinct keys
public static final String LIST_KEY = "SomeActivity_List";
public static final String LIST_LENGTH_KEY = "SomeActivity_ListLength";
/**
* How this method works: It starts by getting a SharedPreferences object,
* which offers an API for persisting data. It then systematically generates
* Strings like "SomeActivity_List1", "SomeActivity_List2", "SomeActivity_List3",
* and so on to use as keys fot the 1st, 2nd, 3rd, etc. elements in the list. Then
* it Data.saveData(), a method defined below in the Data class, in order to give
* each Data object in the ArrayList an opportunity to persist its primitive
* members in the SharedPreferences.
*
* SomeActivity.restoreList() works similarly.
*/
public void saveList() {
SharedPreferences prefs = getPreferences(); //This method is part of the Activity class
SharedPreferences.Editor editor = prefs.getEditor();
//Save the length of the list so that when we restore it, we know how many
//Data objects to recreate.
editor.putInt(LIST_LENGTH_KEY, list.size());
editor.commit();
//This for loop is important - note how we concatenate i to each of the keys to give each element in list a distinct key
for(int i = 0; i < list.size(); i++)
{
String identifier = LIST_KEY + Integer.toString(i); //generate distinct keys
list.get(i).saveData(identifier, prefs);
}
}
public void restoreList() {
SharedPreferences prefs = getPreferences();
int length = prefs.getInt(LIST_LENGTH_KEY);
for(int i = 0; i < length; i++)
{
String identifier = LIST_KEY + Integer.toString(i); //re-generate distinct keys
Data data = new Data();
data.restoreData(identifier, prefs);
list.addLast(data);
}
}
public static class Data {
private int i;
private double j;
private String s;
public static final String I_KEY = "Data_I"
public static final String J_KEY = "Data_J" //strings can really be whatever, as long as they're distinct.
public static final String S_KEY = "Data_K"
/**
* How this method works: The SomeActivity.saveList() method generates a
* unique String ("identifier") for each of the Data objects it contains.
* This method uses that distinct string and makes some more distinct keys
* to store each of Data's primitive members.
*
* restoreData() works similarly when it rebuilds Data objects
*/
public saveData(String identifier, SharedPreferences prefs) {
SharedPreferences.Editor editor = prefs.getEditor();
editor.putInt(I_KEY + identifier, i);
editor.putDouble(J_KEY + identifier, j);
editor.putString(S_KEY + identifier, s);
editor.commit();
}
public restoreData(String identifier, SharedPreferences prefs) {
i = prefs.getInt(I_KEY + identifier);
j = prefs.getDouble(J_KEY + identifier);
s = prefs.getString(S_KEY + identifier);
}
}
}
This approach work recursively. If Data had an ArrayList as one of its fields, for example, it could systematically store all of the values in that list in SharedPreferences as well.
FYI: One of the implications of using SharedPreferences is that if the user uninstalls your app or clears the app data, the stored list will be deleted. Depending on the nature of your data, you may or may not want this behavior.

Categories

Resources