为什么Android平板电脑上的应用程序不能从Url下载视频?
我可以下载视频并保存在sd卡上。当用户多次下载时,它会将其保存在sd卡上,直到内存中有可用空间为止。当文件大小较大时,如果sd卡上没有空间,则不会将下载的文件保存在sd卡上 在我的模拟器中,这一切都很正常。除了我不能在模拟器中播放视频。但下载的视频文件节省了时间和mp4格式。我可以通过将视频文件拉到桌面来查看它 这是发挥和工作良好。也不例外的是抛出和应用程序是非常清楚和相当好。那么这里有什么问题 这是我的代码:为什么Android平板电脑上的应用程序不能从Url下载视频?,android,download,storage,Android,Download,Storage,我可以下载视频并保存在sd卡上。当用户多次下载时,它会将其保存在sd卡上,直到内存中有可用空间为止。当文件大小较大时,如果sd卡上没有空间,则不会将下载的文件保存在sd卡上 在我的模拟器中,这一切都很正常。除了我不能在模拟器中播放视频。但下载的视频文件节省了时间和mp4格式。我可以通过将视频文件拉到桌面来查看它 这是发挥和工作良好。也不例外的是抛出和应用程序是非常清楚和相当好。那么这里有什么问题 这是我的代码: playFromUrlButton=(Button)findViewById(R.i
playFromUrlButton=(Button)findViewById(R.id.play);
playFromUrlButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(VideoSDcard.this, "Download Button is Clicked", Toast.LENGTH_LONG).show();
DownloadFromUrl(url,"mp4");
}
private String DownloadFromUrl(String Url,String format) {
InputStream bis = null;
FileOutputStream fos = null;
long startTime = System.currentTimeMillis();
String fileName="mmData_"+startTime+"."+format;
try {
URL url = new URL(Url);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
ucon.connect();
size=ucon.getContentLength();
directory= new File (path);
System.out.println("File Directory:"+directory);
if(!directory.exists())
{
directory.mkdirs();
}
directory=new File(path+fileName);
/*
* Define InputStreams to read from the URLConnection.
*/
bis = new BufferedInputStream(url.openStream());
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
fos = new FileOutputStream(directory);
byte data[] = new byte[1024];
int current=0;
while ((current = bis.read(data)) != -1)
{
fos.write(data, 0, current);
}
/* Convert the Bytes read to a String. */
fos.flush();
fos.close();
bis.close();
System.out.println("Returns path and File name:"+path+fileName);
return path+fileName;
}
catch (IOException e)
{
Toast.makeText(VideoSDcard.this, "Exception"+e.toString(), Toast.LENGTH_LONG).show();
System.out.println("\n Exception while Downloading"+e.toString());
try
{
fos.flush();
fos.close();
bis.close();
}
catch (IOException e1)
{
Toast.makeText(VideoSDcard.this, "Exception1"+e.toString(), Toast.LENGTH_LONG).show();
e1.printStackTrace();
}
return path+fileName;
}
}
我的日志
06-11 13:52:18.867: I/System.out(10469): Argument Url:http://download.itcuties.com/teaser/itcuties-teaser-480.mp4
06-11 13:52:18.877: I/System.out(10469): Download from URL:http://download.itcuties.com/teaser/itcuties-teaser-480.mp4
06-11 13:52:19.307: I/System.out(10469): AvailableExternalMemorySize:170567680
06-11 13:52:19.307: I/System.out(10469): FileSize-14514169:available-170567680
06-11 13:52:19.317: I/System.out(10469): AvailableExternalMemorySize:170567680
06-11 13:52:19.327: I/System.out(10469): Video path:/mnt/sdcard/MyVideo/
06-11 13:52:19.327: I/System.out(10469): File Directory:/mnt/sdcard/MyVideo
06-11 13:52:19.327: I/System.out(10469): Check : /mnt/sdcard/MyVideo/ : mmData_1370938938871.mp4
06-11 13:53:19.539: I/System.out(10469): Returns path and File name:/mnt/sdcard/MyVideo/mmData_1370938938871.mp4
为什么它不能在Android平板电脑上播放?您正在主UI线程上执行网络活动。如果你的平板电脑运行的是Android>=v12,它将抛出一个异常-你的模拟器可能会工作,因为它是
如果你阻止了主线程,用户体验就会受到影响(应用程序会出现冻结),10秒后系统将“Android无响应”(ANR),所以你会问为什么你的视频没有在平板电脑上播放,对吗?是的,可能是互联网断开了?首先检查它们是否下载正确,将字节保存到sd卡时没有任何错误。第二件事可能是您用于测试的设备不支持文件设置/格式。完全下载sd卡时没有任何错误。那我真的不明白有什么问题。不幸的是,活动已经停止了。当我在平板电脑上测试它时,我看到的就是这样。但我可以在Emulator中运行它。在emulator中,下载文件后,toast显示为“sd卡上没有空间,所以异常”,然后还显示URL。我在试接循环中使用了吐司。请任何人帮助/建议我!