動画をダウンロードして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 タブレットで再生できないのはなぜですか?