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

发布于 2024-12-09 14:04:17 字数 460 浏览 0 评论 0原文

我正在尝试在列表视图中显示 SD 卡中创建的文件夹中的视频文件。我正在使用内容提供程序,例如:

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

但它读取存储在 SD 卡中的所有视频文件。我只想访问存储在 SD 卡文件夹中的文件。我也用过:

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

但它给出了错误。有没有办法在 ManagedQuery() 中包含文件夹路径或以其他方式在列表视图中显示文件夹中的视频文件?

I am trying to display video files in listview from folder created in sdcard. I am using content provider such as:

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

But it reads all video files stored in sdcard. I want to access only files stored in folder in sdcard. I have also used:

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

But it gives error. Is there any way to include path of folder in managedQuery() or any other way to display video files from folder in listview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2024-12-16 14:04:17

试试这个,

 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]);
   }

Try this,

 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]);
   }
○愚か者の日 2024-12-16 14:04:17

尝试从资源获取文件然后放入 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

Try to get file from resources then put in 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
静谧幽蓝 2024-12-16 14:04:17

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

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

We can use managedquerry as below to get the videos from a specified folder on sd-card....

videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
    videoProjection, MediaStore.Images.Media.DATA + " like ? ",
    new String[]{"%M-Videos%"}, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文