Android ContentResolver insert

In this Android tip, I will give you an example of using the insert() method of the ContentResolver class to add a new bookmark entry to the browser of an Android phone. The insert() method has the following syntax:

insert(Uri url, ContentValues values)

The url argument is a table that the new entry will be inserted to. You can refer to the bookmark table by using the sub-class BOOKMARKS_URI of the Browser class (Browser.BOOKMARKS_URI). The values argument is a collection of pairs of key (column name) and value. You can access a name of a column by using the Browser.BookmarkColumns. For example, to refer to the icon field of the bookmark table, you will write Browser.BookmarkColumns.FAVICON.

Now to demonstrate the example of using insert() method of the ContentResolver class to add a new bookmark entry to the browser, create a new Android project and then edit the activity_main.xml file to add a Button as shown below.

<LinearLayout xmlns:android=""

     android:text="Add a bookmark"


After that, in the MainActivity class, you create a new method called addBookmark(). This method is called to add a new bookmark entry to the bookmark table.

public void addBookmark(View view){
//get ContentResolver
ContentResolver cr=getContentResolver();
//columns the values will be inserted to
String[] projection={Browser.BookmarkColumns.CREATED,Browser.BookmarkColumns.DATE,
Browser.BookmarkColumns.FAVICON, Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL,
Browser.BookmarkColumns.VISITS, Browser.BookmarkColumns.BOOKMARK

//create ContentValues collection object
ContentValues values = new ContentValues();
//put pairs of key (column name) and values in the collection
values.put(projection[0], System.currentTimeMillis());
values.put(projection[1], System.currentTimeMillis());
Bitmap icon=BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] buffer=baos.toByteArray();
values.put(projection[3], "C++ exercises");
values.put(projection[5], 2);
//insert a new bookmark entry to the bookmark table
cr.insert(Browser.BOOKMARKS_URI, values);


Before running the example application, you need to add a permission to the AndroidManifest.xml file to write data to the bookmark table on an Android device.

  <uses-permission android:name="" />


Posted by: Dara | post date: 08-09-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.