我可以下载视频并将其保存在 sd 卡上。当用户多次下载时,它会将其保存在 sd 卡上,直到内存中有可用空间。当文件较大时,如果 sd 卡上没有空间,则不会将下载的文件保存在 sd 卡上。
这在我的模拟器中一切正常。除了我无法在模拟器中播放视频。但是下载的视频文件节省了时间和 mp4 格式。我可以通过将视频文件拉入桌面来查看视频文件。
它正在播放和工作正常。抛出也不例外,应用程序非常清晰且非常好。那么这里有什么问题呢?
这是我的代码:
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 平板电脑上播放?