如何获取可在我的应用安装的Android设备上找到的所有视频及其路径的列表?
编辑: 我正在尝试制作一个简单的视频播放器,因此想法是显示可以在设备上找到的所有视频,并在列表项目点击上播放。 唯一的问题是我不知道如何获取有关视频的信息,所以如果有人可以帮助我,我会非常感激。
答案 0 :(得分:3)
如果有人仍在寻找答案,请尝试此方法。此方法将返回所有媒体的路径列表。
public ArrayList<String> getAllMedia() {
HashSet<String> videoItemHashSet = new HashSet<>();
String[] projection = { MediaStore.Video.VideoColumns.DATA ,MediaStore.Video.Media.DISPLAY_NAME};
Cursor cursor = getContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
try {
cursor.moveToFirst();
do{
videoItemHashSet.add((cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
}while(cursor.moveToNext());
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<String> downloadedList = new ArrayList<>(videoItemHashSet);
return downloadedList;
}
答案 1 :(得分:2)
String[] projection = { MediaStore.Video.Media._ID};
Cursor cursor = new CursorLoader(contexts, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection,
null, // Return all rows
null, null).loadInBackground();
获得光标后,您可以使用视频ID迭代它并获取所有视频。
答案 2 :(得分:1)
请尝试以下代码。
private void getVideoList() {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Video.VideoColumns.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
ArrayList<String> pathArrList = new ArrayList<>();
if (cursor != null) {
while (cursor.moveToNext()) {
pathArrList.add(cursor.getString(0));
}
cursor.close();
}
Log.e("all path",pathArrList.toString());
}