android-讀取MediaProvider

來源:互聯網
上載者:User

標籤:過多   bsp   main   使用   rgs   圖庫   base   nal   []   

 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

android-讀取MediaProvider

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.