android獲取本地視頻列表

小云
181
2023-09-23 08:44:24

要獲取本地視頻列表,可以使用MediaStore類(lèi)來(lái)查詢(xún)MediaStore.Video.Media表。以下是一個(gè)示例代碼:

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
public class LocalVideoFetcher {
public static List<String> getVideoList(Context context) {
List<String> videoList = new ArrayList<>();
ContentResolver contentResolver = context.getContentResolver();
// 查詢(xún)視頻
String[] projection = {MediaStore.Video.Media.DATA};
Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String videoPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
videoList.add(videoPath);
}
cursor.close();
}
return videoList;
}
}

在調(diào)用上述方法時(shí),需要傳入一個(gè)Context對(duì)象。例如,在A(yíng)ctivity中獲取本地視頻列表,可以這樣調(diào)用:

List<String> videos = LocalVideoFetcher.getVideoList(this);

請(qǐng)注意,獲取本地視頻列表可能需要在A(yíng)ndroidManifest.xml文件中添加以下權(quán)限:

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

0