【问题标题】:VLC: Cant Play Video from getExternalStorageDirectory()VLC:无法从 getExternalStorageDirectory() 播放视频
【发布时间】:2018-01-23 03:38:00
【问题描述】:

我已将我的视频文件保存到我的存储中,并尝试使用 Intent 播放我的应用程序中的视频。在其他播放器上,该文件播放正常,但当我尝试使用 VLC 播放该文件时,出现以下错误;

播放错误 Vlc 遇到此媒体错误。请尝试 刷新媒体库。

位置 /storage/emulated/0/myFolder/file.mov 无法播放。

保存方法

private String SaveToDir(String DownloadUrl) {

    try {

        File dir = new File(Environment
                .getExternalStorageDirectory(),
                "myFolder");
        if (dir.exists() == false) {
            dir.mkdirs();
        }

        URL url = new URL(DownloadUrl);
        String fileName = DownloadUrl.substring(DownloadUrl.lastIndexOf('/') + 1);
        File file = new File(dir, fileName);
        if (file.exists()) {
            return file.getAbsolutePath();
        } else {
            long startTime = System.currentTimeMillis();
            Log.d("DownloadManager", "download url:" + url);
            Log.d("DownloadManager", "download file name:" + fileName);

            URLConnection uconn = url.openConnection();
            uconn.setReadTimeout(TIMEOUT_CONNECTION);
            uconn.setConnectTimeout(TIMEOUT_SOCKET);

            InputStream is = uconn.getInputStream();
            BufferedInputStream bufferinstream = new BufferedInputStream(is);

            BufferedOutputStream outStream = null;
            outStream = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buf = new byte[5000];
            int len;
            while ((len = bufferinstream.read(buf)) > 0) {
                outStream.write(buf, 0, len);
            }

            outStream.flush();
            outStream.close();
            makeFileAvailable(file);
            Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + "sec");
            int dotindex = fileName.lastIndexOf('.');
            if (dotindex >= 0) {
                fileName = fileName.substring(0, dotindex);

            }
            return file.getAbsolutePath();
        }
    } catch (IOException e) {
        Log.d("DownloadManager", "Error:" + e);
        e.printStackTrace();
        return "";
    }

}

意图

 String responselink = SaveToDir(getImgUrl());
 String type = FileUtils.getMimeType(responselink);

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(responselink));
 intent.setDataAndType(Uri.parse(responselink), type);                              
 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);                                
 intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
 generalPropListener.getSelfContext().startActivity(intent);

【问题讨论】:

    标签: android video file-permissions


    【解决方案1】:

    尝试在 responseLink 前添加“file://”。

    【讨论】:

    • 可以,但 Android 7.0 可能存在问题
    • 那么你必须使用FileProvider。这很乏味,但并不难。
    猜你喜欢
    • 2013-04-04
    • 2016-07-17
    • 1970-01-01
    • 2018-10-15
    • 2020-02-24
    • 2012-09-07
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    相关资源
    最近更新 更多