2017-08-15 121 views
2

我已将视频文件保存到我的存储空间,并尝试使用Intent播放来自我的应用的视频。在其他播放器上,文件播放正常,但是当我试图播放该文件时与VLC它给了我下面的错误;VLC:从getExternalStorageDirectory()播放视频

播放错误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); 

回答

0

尽量在前面加上 “文件://” 来responseLink。

+0

是的,它会工作,但可能有一个与Android 7.0的问题 – Mysterious

+0

然后,你将不得不使用[FileProvider](https://developer.android.com/reference/android/support/v4/content/FileProvider的.html)。这很乏味,但并不难。 – cwbowron