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="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:orientation="vertical"
   >

   <Button
     android:id="@+id/bt_add"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Add a bookmark"
     android:onClick="addBookmark"
      />
  

</LinearLayout>


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.

  @SuppressLint("NewApi")
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[2],buffer);
values.put(projection[3], "C++ exercises");
values.put(projection[4],"http://www.worldbestlearningcenter.com/index_files/cpp-tutorial-variables_datatypes_exercises.htm");
values.put(projection[5], 2);
values.put(projection[6],1);
//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="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS" />
  

Android-ContentResolver-insert

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.