2011-10-14 61 views
3

我正嘗試在sdcard中創建的文件夾中顯示listview中的視頻文件。我正在使用內容提供商如:如何訪問Android應用程序中sd卡文件夾中的視頻文件

videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
proj, null, null, null); 

但它讀取存儲在SD卡中的所有視頻文件。我只想訪問存儲在sdcard文件夾中的文件。我也用過:

Uri a = Uri.parse(Environment.getExternalStorageDirectory()+"/myfolder"); 
videocursor = managedQuery(a,proj, null, null, null); 

但它給出錯誤。有沒有辦法在managedQuery()中包含文件夾的路徑或者其他任何方式來顯示來自listview中文件夾的視頻文件?

回答

3

試試這個,

String[] fileList; 
    File videoFiles = new File(Environment.getExternalStorageDirectory()+"/myfolder"); 

    if(videoFiles.isDirectory()) 
    { 
     fileList=videoFiles.list(); 
    } 

    for(int i=0;i<fileList.length;i++) 
    { 
     Log.e("Video:"+i+" File name",fileList[i]); 
    } 
+1

感謝它成功地工作! – Larik

2

嘗試從資源再放入Uri.fromFile()獲取文件:

private boolean saveAs(int resource) { // save your file in sd card 
     File root = Environment.getExternalStorageDirectory(); 
     String fileName = "your_file.png"; 
     InputStream input = getBaseContext().getResources().openRawResource(resource); 
     String path = root.getAbsolutePath(); 
     FileOutputStream save; 
     byte[] buffer = null; 
     int size = 0; 
     try { 
      size = input.available(); 
      buffer = new byte[size]; 
      input.read(buffer); 
      input.close(); 
     } catch (IOException e) { 
      return false; 
     } 
     boolean exists = (new File(path)).exists(); 
     if (!exists) { 
      new File(path).mkdirs(); 
     } 

     try { 
      File deleteFile = new File(path + "/" + fileName); 
      if (deleteFile.exists()) { 
       deleteFile.delete(); 
      } 
      save = new FileOutputStream(path + "/" + fileName); 
      save.write(buffer); 
      save.flush(); 
      save.close(); 
     } catch (FileNotFoundException e) { 
      return false; 
     } catch (IOException e) { 
      return false; 
     } 

     return true; 
    } 
} 

    Uri.fromFile(new File(Environment.getExternalStorageDirectory(), 
           "your_file.png")) // get it from Uri 
2

我們可以用managedquerry如下從指定的文件夾獲取視頻在SD卡上......

videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
    videoProjection, MediaStore.Images.Media.DATA + " like ? ", 
    new String[]{"%M-Videos%"}, null); 
相關問題