如何从Android应用程序中的SD卡文件夹访问视频文件

时间:2011-10-14 12:25:14

标签: android listview sd-card

我正在尝试在sdcard中创建的文件夹中显示listview中的视频文件。我正在使用内容提供商,例如:

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 个答案:

答案 0 :(得分: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 :(得分: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 :(得分:2)

我们可以使用如下的managedquerry从SD卡上的指定文件夹中获取视频....

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