要獲取本地視頻列表,可以使用MediaStore類來查詢MediaStore.Video.Media表。以下是一個示例代碼:
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();
// 查詢視頻
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;
}
}
在調用上述方法時,需要傳入一個Context對象。例如,在Activity中獲取本地視頻列表,可以這樣調用:
List<String> videos = LocalVideoFetcher.getVideoList(this);
請注意,獲取本地視頻列表可能需要在AndroidManifest.xml文件中添加以下權限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />