Content Provider in Android

An android we used File , Data Base for Stored data permanently. These file and Database are accessible only to its application . Others application and packages can not access this. There fore android provide Content Provider  to shared data among packages and application. Some android shared contents are following
  1. Contact Number 
  2. Media Storage (audio, videos, and images)
  3. Browser bookmarks.
That all are built in content providers. We develop  own content provider to extend the base class of content providers.
Now we used content provider to access built-in Contents
 Fist of all we want to access all all images of Phone which are stored in Sd card.

public class MainActivity extends Activity {
 private String TAG="android.imges";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Uri Images=Uri.parse("android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI");
        Cursor cursor =   managedQuery(Images, null, null, null, null);

// All images path   
       while (cursor.moveToNext()) {
                  String path=  cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
                  log.d(TAG, path);
}
                 
         
  • First There are used built-in Uri to access shared contents . some are given below to access desired contents
we used the image Uri to access it. you can use any of the following
  1.  Browser.BOOKMARKS_URI 
  2. Browser.SEARCHES_URI
  3. CallLog.CONTENT_URI
  4. MediaStore.Images.Media.INTERNAL_CONTENT_URI
  5. MediaStore.Images.Media.EXTERNAL_CONTENT_URI 
  6. Settings.CONTENT_URI
  •  Second we discuss managQuery method which return Cursor object . cursor object contain all retrieved data. and we can easily navigate in cursor object to retrieve all are specific record.
  • Third we now retrieve path of every images from cursor object using while loop and diplay in LogCat . 
 The above code display the path of every image which are stored in android phone sd card. Now we can develop gallery , image switcher and others application where we required all images of internal storage or external storage.

     
 

      Post a Comment

      0 Comments