有人可以告诉我如何对android URL进行编码/解码(在android中),该youtube URL是间接的直接URL,以便从Youtube下载视频?我尝试了可能的方法来转换实际的间接URL并对其进行编码,以提供另一个包含十六进制值的URL。但是此方法不起作用,因为我无法使用我的下载管理器类解析URL来下载视频。感谢本论坛中所有发展中的天才,以帮助像我这样的初学者。预先感谢那些给我解决方案的人。
PS:此操作仅供个人使用,不能用于商业用途。另外,出于好奇,我正在这样做。
最佳答案
3个步骤:
步骤1:检查YouTube的源代码(HTML),您将获得如下所示的链接(http%253A%252F%252Fo-o.preferred.telemar-cnf1.v18.lscache6.c.youtube.com%252Fvideoplayback。 ..);
步骤2:对URL进行解码(删除%2B,%25等代码),使用以下代码创建解码器:http://www.w3schools.com/tags/ref_urlencode.asp并使用函数Uri.decode(url)替换无效的转义八位字节;
步骤3:使用代码下载流:
URL u = null;
InputStream is = null;
try {
u = new URL(url);
is = u.openStream();
HttpURLConnection huc = (HttpURLConnection)u.openConnection();//to know the size of video
int size = huc.getContentLength();
if(huc != null){
String fileName = "FILE.mp4";
String storagePath = Environment.getExternalStorageDirectory().toString();
File f = new File(storagePath,fileName);
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int len1 = 0;
if(is != null){
while ((len1 = is.read(buffer)) > 0) {
fos.write(buffer,0, len1);
}
}
if(fos != null){
fos.close();
}
}
}catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if(is != null){
is.close();
}
}catch (IOException ioe) {
// just going to ignore this one
}
}
关于android - youtube视频下载android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25499544/