LoginSignup
1
1

More than 5 years have passed since last update.

Store an arbitrary Bundle object into a SQLite database

Last updated at Posted at 2012-02-11

Note that the technique using Parcel is not recommended to use to store a object into a persistent storage.

store_bundle_in_sqlite3.java
/* store a bundle */
Bundle bundle = // get a bundle from somewhere
final Parcel parcel = Parcel.obtain();
bundle.writeToParcel(parcel, 0);
byte[] bundleBytes = parcel.marshall();
parcel.recycle();

ContentValues values = new ContentValues();
values.put("bundle", bundleBytes);
SQLiteDatabase db = // create a db object
db.insert("table", null, values);

/* restore a bundle */
Cursor cur = // get a cursor of your db
byte[] bundleBytes = cur.getBlob(cur.getColumnIndex("bundle"));
final Parcel parcel = Parcel.obtain();
parcel.unmarshall(bundleBytes, 0, bundleBytes.length);
parcel.setDataPosition(0);
Bundle bundle = (Bundle) parcel.readBundle();
parcel.recycle();

You can get complete code from https://github.com/peo3/IntentSpooler/blob/master/src/jp/peo3/android/intentspooler/database/IntentDatabaseAdapter.java

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1