2013-11-15 83 views
4

如何使用mediastore獲取存儲在特定文件夾中的視頻文件列表?目前我正在使用此代碼如何獲取Android中特定文件夾中的視頻文件列表?

package com.example.videolisttest; 

import android.app.Activity; 
import android.content.Context; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    private Cursor videocursor; 
    private int video_column_index; 
    ListView videolist; 
    int count; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     init_phone_video_grid(); 
    } 

    private void init_phone_video_grid() { 
     System.gc(); 
     String[] proj = { MediaStore.Video.Media._ID, 
       MediaStore.Video.Media.DATA, 
       MediaStore.Video.Media.DISPLAY_NAME, 
       MediaStore.Video.Media.SIZE }; 
     videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
       proj, null, null, null); 
     count = videocursor.getCount(); 
     videolist = (ListView) findViewById(R.id.PhoneVideoList); 
     videolist.setAdapter(new VideoAdapter(getApplicationContext())); 
     videolist.setOnItemClickListener(videogridlistener); 
    } 

    private OnItemClickListener videogridlistener = new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, 
       long id) { 
      System.gc(); 
      video_column_index = videocursor 
        .getColumnIndexOrThrow(MediaStore.Video.Media.DATA); 
      videocursor.moveToPosition(position); 
      String filename = videocursor.getString(video_column_index); 
      /* Intent intent = new Intent(MainActivity.this, ViewVideo.class); 
        intent.putExtra("videofilename", filename); 
        startActivity(intent);*/ 
      Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    public class VideoAdapter extends BaseAdapter { 
     private Context vContext; 

     public VideoAdapter(Context c) { 
      vContext = c; 
     } 

     public int getCount() { 
      return count; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      System.gc(); 
      TextView tv = new TextView(vContext.getApplicationContext()); 
      String id = null; 
      if (convertView == null) { 
       video_column_index = videocursor 
         .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME); 
       videocursor.moveToPosition(position); 
       id = videocursor.getString(video_column_index); 
       video_column_index = videocursor 
         .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE); 
       videocursor.moveToPosition(position); 
       id += " Size(KB):" + videocursor.getString(video_column_index); 
       tv.setText(id); 
      } else 
       tv = (TextView) convertView; 
      return tv; 
     } 
    } 
}  

但是這種方法掃描整個SD卡。我想掃描一個特定的文件夾。我不想使用filefilter方法。請使用mediastore共享任何方法。

回答

10

您可以使用此代碼得到特定文件夾的視頻爲:

String selection=MediaStore.Video.Media.DATA +" like?"; 
      String[] selectionArgs=new String[]{"%FolderName%"}; 
      videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
        parameters, selection, selectionArgs, MediaStore.Video.Media.DATE_TAKEN + " DESC"); 

它的作品對我來說就像一個魅力。

+0

這是完美的作品。兄弟,你能解釋一下這幾行嗎? - 「喜歡?」; ,參數,「DESC」和「%」符號 –

+2

String [] parameters = {MediaStore.Video.Media._ID}就像sql語句,你可以指定媒體數據的位置.Desc是最近第一次顯示進入列表。 –

+0

但我們不能相信這一點,因爲它是一種搜索機制。如果您有兩個名稱幾乎相同的目錄,它會合並結果。 –

1

如果您知道具體的文件夾,使用:

.getExternalFilesDir 
2
private List<String> path_vid; 
public void searchVid(File dir) { 
    String pattern = ".mp4"; 
      //Get the listfile of that flder 
    final File listFile[] = dir.listFiles(); 

    if (listFile != null) { 
     for (int i = 0; i < listFile.length; i++) { 
      final int x = i; 
      if (listFile[i].isDirectory()) { 
       walkdir(listFile[i]); 
      } else { 
       if (listFile[i].getName().endsWith(pattern)) { 
        // Do what ever u want, add the path of the video to the list 
         path_vid.add(listFile[i]); 
       } 
      } 
     } 
    } 
} 

此功能是遞歸的,和搜索西元將它設置成一個列表中,但前提是從especified文件夾中查找,如果u想搜索西元到SD卡,使用:

File sdCard = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); 
//For example: 
//File vidsFolder= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Videos"); 
searchVid(sdCard); 
if(path_vid.size()>0){ 
    //Convert list into array 
    String[] array = path_vid.toArray(new String[path_vid.size()]); 
    //Create Adapter 
    ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item, array); 
    //Set adapter to videlist 
    videolist.setAdapter(adapter); 
}else{ 
    //No vids found 
    exit(); 
} 
+0

謝謝你的回答,但我的問題是如何與Mediastore做到這一點? –

+0

這個答案可以幫助你:http://stackoverflow.com/questions/10503840/how-to-get-all-videos-on-android-device –

相關問題