2012-05-08 59 views
3

如何獲取可在安裝了我的應用的Android設備上找到的所有視頻及其路徑列表?如何獲取Android設備上的所有視頻

編輯: 我想製作一個簡單的視頻播放器,所以這個想法是顯示設備上可以找到的所有視頻,並在列表項上單擊播放它。 唯一的問題是我不知道如何獲得有關視頻的信息,所以如果有人能幫助我,我會非常感激。

+0

我沒有嘗試過任何東西,真的不知道從哪裏開始.. – sth4

+0

我真的不能找到在互聯網,這將有助於我的東西,所以我還沒有試過沒任何東西。現在我完成了應用程序的其餘部分,但只是錯過了挑選視頻。 – sth4

+0

我在** Related **欄中找到了這個,在那裏 - > http://stackoverflow.com/q/2911124/1267661還有更多有用的鏈接,以及Preet Sangha的。祝你好運! – Sam

回答

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獲取所有視頻。

+0

不工作,如果文件夾以編程方式創建https://stackoverflow.com/questions/48934159/mediastore-not-working-when-folder-programmatically-created請檢查此 –

2

如果有人仍在尋找答案,請嘗試使用此方法。此方法將返回所有媒體的路徑列表。

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; 
} 
+0

不適用於我以編程方式創建的文件夾stackoverflow.com/問題/ 48934159/...請檢查此 –

0

請嘗試下面的代碼。

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()); 
} 
相關問題