1.MediaProvider storage of media files in the phone, using SQLite database to store pictures, video, audio and other multimedia files for the video player, music player, gallery use. With root access to the ADB shell, use sqlite3 to open a database on the /data/data/com.android.providers.media/databases on the phone. The database that starts with external stores the SD card media information, one card corresponds to one, so if the phone uses too many cards there will be multiple databases. The database that starts with internal stores the media information for the phone's internal memory. Because the average user cannot access the phone's internal memory, and the two database structures are roughly the same, you just need to focus on the external database.
2. Configure read and Write permissions:
<uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE " />&NBSP;&NBSP;
<uses-permission android:name="Android.permission.WRITE_MEDIA_STORAGE" />
code declaration:
Private Contentproviderclient Mprovider; private static final string[] Files_prescan_projection = new string[]{
mediastore.files.filecolumns._id,//0
MediaStore.Files.FileColumns.DATA,//1
};
Private contentvalues tovalues (int i) {
Contentvalues map = new Contentvalues ();
Map.put (MediaStore.MediaColumns.DATA, "/zcx" +i);
Map.put (MediaStore.MediaColumns.TITLE, "zcxzcx");
Map.put (mediastore.mediacolumns.size,5);
Map.put (MediaStore.MediaColumns.MIME_TYPE, "Tiantian");
return map;
}
Main ():
Mprovider = Getcontentresolver ()
. Acquirecontentproviderclient (mediastore.authority);
Uri Mfilesuri = MediaStore.Files.getContentUri ("external");
Cursor c = null; String where = mediastore.files.filecolumns._id + ";?"; /+ "and" + MediaStore.Files.FileColumns.DATA + "=?";
Uri Limituri = Mfilesuri.buildupon (). Appendqueryparameter ("Limit", "n"). Build ();
string[] Selectionargs = Null;selectionargs = new string[] {""};long lastid=160000;//, "/zcx3"
Contentvalues values = new contentvalues (); values = tovalues (0);
Contentvalues values2 = new Contentvalues (); values2 = Tovalues (1);
list<contentvalues> list = new arraylist<contentvalues> ();
int cou = 100;
while ((cou--)!=0) {
List.add (Tovalues (cou));
}
try {
contentvalues[] Valuesarray = new contentvalues[list.size ()];
Valuesarray = List.toarray (Valuesarray);
Mprovider.insert (mfilesuri,values);
Mprovider.insert (Mfilesuri,values2);
st = System.currenttimemillis ();
Mprovider.bulkinsert (Mfilesuri,valuesarray);
End = System.currenttimemillis ();
} catch (RemoteException e) {
LOG.D ("Zcxdatabase2", "remoteexception =");
E.printstacktrace ();
}
while (true) {
Selectionargs[0] = "" + LastID;
if (c! = null) {
C.close ();
c = null;
}
try {
c = Mprovider.query (Limituri, Files_prescan_projection,
Where, Selectionargs, mediastore.files.filecolumns._id, NULL);
} catch (RemoteException e) {
E.printstacktrace ();
}
if (c = = null) {
Break
}
int num = C.getcount ();
if (num = = 0) {
Break
}
while (C.movetonext ()) {
Long rowId = C.getlong (0);
String Path = c.getstring (1);
LOG.D ("Zcxdatabase", "rowId =" +rowid+ "path =" +path);
LastID = rowId;
Only consider entries with absolute path names.
This allows storing URIs in the database without the
Media scanner removing them.
}
Break
}
Reference Document: Https://www.cnblogs.com/linlf03/p/3477077.html
android-Read Mediaprovider