android-读取MediaProvider

 1.MediaProvider存储手机中的媒体文件,用 SQLite 数据库存储图片、视频、音频等多媒体文件的信息,供视频播放器、音乐播放器、图库使用。以 root 权限进入 adb shell,使用 sqlite3 打开位于手机上 /data/data/com.android.providers.media/databases 上的一个数据库。以 external 开头的数据库存储的是 SD 卡媒体信息,一张卡对应一个,所以如果手机使用过多张卡会有多个数据库。以 internal 开头的数据库存储手机内部存储器的媒体信息。因为一般用户无法访问手机内部存储器,而且这两个数据库结构是大体上是相同的,所以只需要关注 external 数据库即可。

2.配置读写权限:

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

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

代码声明:

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", "1000").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;
}
 

参考文档:https://www.cnblogs.com/linlf03/p/3477077.html

猜你喜欢

转载自www.cnblogs.com/zCoderJoy/p/8975846.html
今日推荐