Method of Android Using ContentProvider to Obtain Local Data

  • 2021-09-05 00:52:50
  • OfStack

In the previous article, we mentioned that the main function of ContentProvider is to provide shared data for other applications to use. Within the Android system, shared data is also provided. Check the android. provider package and find the following shared data

Local multimedia (pictures, audio and video, etc.) Address book contacts Call record SMS Record ...

Of course, these data must be authorized by the user before it can be called, so please apply for the corresponding authority before calling (above 6.0 system)

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1000);

Get data

Query method of getContentResolver ()


query(@RequiresPermission.Read @NonNull Uri uri,
    @Nullable String[] projection, @Nullable String selection,
    @Nullable String[] selectionArgs, @Nullable String sortOrder)

In which the meaning of parameters

uri path of uri target

The pictures are MediaStore.Images.Media.EXTERNAL_CONTENT_URI (external) and MediaStore.Images.Media.INTERNAL_CONTENT_URI (internal)

Video is MediaStore.Video.Media.EXTERNAL_CONTENT_URI

The audio is MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

Address Book ContactsContract.Contacts.CONTENT_URI

Call record CallLog.Calls.CONTENT_URI

SMS Record Telephony.Sms.CONTENT_URI

projection query results need which 1 attributes, null return all selection query criteria Parameters of selectionArgs Corresponding to Query Criteria sortOrder permutation condition

Take pictures as an example

If you don't know which fields are available, you can use the < code > MediaStore.Images.ImageColumns < /code > Find all the fields, and you can also get all the fields in the following ways


String [] columnNames = mCursor.getColumnNames();

Correct posture


// Of the picture type Uri Path 
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// According to the modification time of the picture, filter out jpeg And png Picture of type 
Cursor mCursor =mContext.getContentResolver()
  .query(uri,null,MediaStore.Images.ImageColumns.MIME_TYPE + "=? or "+ MediaStore.Images.ImageColumns.MIME_TYPE + "=?",
  new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED); 
if(mCursor!=null){

while(mCursor.moveToNext()){

// Get the picture path 
String path = mCursor.getString(mCursor
    .getColumnIndex(MediaStore.Images.ImageColumns.DATA));
// Get Modified Date 
long date = mCursor.getLong(mCursor
    .getColumnIndex(MediaStore.Images.ImageColumns.DATE_MODIFIED));

//do something
}
}

Because the amount of data may be very large, it is recommended to put queries and other operations on child threads to avoid blocking the main thread

Using ContentProvider, we can not only get local pictures, realize our self-defined map selection function, but also get address book, realize self-defined contact interface, and also get short message records to complete the corresponding page function effect.


Related articles: