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);
是的,它會工作,但可能有一個與Android 7.0的問題 – Mysterious
然後,你將不得不使用[FileProvider](https://developer.android.com/reference/android/support/v4/content/FileProvider的.html)。這很乏味,但並不難。 – cwbowron