web-dev-qa-db-ja.com

ANDROID:ビデオファイルをSDカードにダウンロードするにはどうすればよいですか?

Webサイトに.MP4形式のビデオファイルがあり、ユーザーがリンクをクリックしてビデオをSDカードにダウンロードできるようにしたいと考えています。これを行う簡単な方法はありますか?私は現在このコードを持っていますが、機能していません...何が間違っているのかわかりません。助けてくれてありがとう!

import Java.io.BufferedInputStream;
import Java.io.File;
import Java.io.FileOutputStream;
import Java.io.IOException;
import Java.io.InputStream;
import Java.net.URL;
import Java.net.URLConnection;

import org.Apache.http.util.ByteArrayBuffer;

import Android.app.Activity;
import Android.os.Bundle;
import Android.util.Log;

public class VideoManager extends Activity {
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);}



        private final String PATH = "/sdcard/download/";  //put the downloaded file here


        public void DownloadFromUrl(String VideoURL, String fileName) {  //this is the downloader method
                try {
                        URL url = new URL("http://www.ericmoyer.com/episode1.mp4"); //you can write here any link
                        File file = new File(fileName);

                        long startTime = System.currentTimeMillis();
                        Log.d("VideoManager", "download begining");
                        Log.d("VideoManager", "download url:" + url);
                        Log.d("VideoManager", "downloaded file name:" + fileName);
                        /* Open a connection to that URL. */
                        URLConnection ucon = url.openConnection();

                        /*
                         * Define InputStreams to read from the URLConnection.
                         */
                        InputStream is = ucon.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);

                        /*
                         * Read bytes to the Buffer until there is nothing more to read(-1).
                         */
                        ByteArrayBuffer baf = new ByteArrayBuffer(50);
                        int current = 0;
                        while ((current = bis.read()) != -1) {
                                baf.append((byte) current);
                        }

                        /* Convert the Bytes read to a String. */
                        FileOutputStream fos = new FileOutputStream(PATH+file);
                        fos.write(baf.toByteArray());
                        fos.close();
                        Log.d("VideoManager", "download ready in"
                                        + ((System.currentTimeMillis() - startTime) / 1000)
                                        + " sec");

                } catch (IOException e) {
                        Log.d("VideoManager", "Error: " + e);
                }

        }
}
16
IZI_Shadow_IZI

メモリが不足していませんか?ビデオファイルは非常に大きいと思います-ファイルに書き込む前にバッファリングしています。

あなたのサンプルコードがインターネットのいたるところにあることは知っていますが、ダウンロードするのは悪いことです!これを使って:

private final int TIMEOUT_CONNECTION = 5000;//5sec
private final int TIMEOUT_SOCKET = 30000;//30sec


            URL url = new URL(imageURL);
            long startTime = System.currentTimeMillis();
            Log.i(TAG, "image download beginning: "+imageURL);

            //Open a connection to that URL.
            URLConnection ucon = url.openConnection();

            //this timeout affects how long it takes for the app to realize there's a connection problem
            ucon.setReadTimeout(TIMEOUT_CONNECTION);
            ucon.setConnectTimeout(TIMEOUT_SOCKET);


            //Define InputStreams to read from the URLConnection.
            // uses 3KB download buffer
            InputStream is = ucon.getInputStream();
            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
            FileOutputStream outStream = new FileOutputStream(file);
            byte[] buff = new byte[5 * 1024];

            //Read bytes (and store them) until there is nothing more to read(-1)
            int len;
            while ((len = inStream.read(buff)) != -1)
            {
                outStream.write(buff,0,len);
            }

            //clean up
            outStream.flush();
            outStream.close();
            inStream.close();

            Log.i(TAG, "download completed in "
                    + ((System.currentTimeMillis() - startTime) / 1000)
                    + " sec");5
36

特に外部ストレージへのパスを配線しないでください。多くのデバイスでパスが間違っています。 Environment.getExternalStoragePath()を使用して、外部ストレージのルートを取得します(may _/sdcard_または_/mnt/sdcard_など)。

Environment.getExternalStoragePath()から取得したFileオブジェクトを使用して、必ずサブディレクトリを作成してください。

そして最後に、「しかし、それは機能しない」とだけ言うのではありません。あなたの場合、「しかし機能しない」とはどういう意味かわかりません。その情報がなければ、あなたを助けることは非常に困難です。

24
CommonsWare