facebook twitter hatena line email

「Android/VideoView/ダウンロード」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行1: 行1:
 +
==サンプル==
 +
<pre>
 +
class VideoDownloaderConnection extends AsyncTask<Void, Long, Boolean> {
 +
    private String TAG = "VideoDownloaderConnection";
 +
    private Context context;
 +
    String url = "";
 +
    public VideoDownloaderConnection(Context context) {
 +
        this.context = context;
 +
    }
 +
    public void setUrl(String url) {
 +
        this.url = url;
 +
    }
 +
    @Override
 +
    protected void onPreExecute() {
 +
        super.onPreExecute();
 +
    }
 +
    @Override
 +
    protected Boolean doInBackground(Void... params) {
 +
        URL u = null;
 +
        InputStream is = null;
 +
        try {
 +
            u = new URL(url);
 +
            is = u.openStream();
 +
            HttpURLConnection huc = (HttpURLConnection)u.openConnection();
 +
            int size = huc.getContentLength();
 +
            if(huc != null) {
 +
                String fileName = "FILE.mp4";
 +
                String storagePath = context.getExternalCacheDir().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) {
 +
            }
 +
        }
 +
        return true;
 +
    }
 +
    @Override
 +
    protected void onProgressUpdate(Long... values) {
 +
        super.onProgressUpdate(values);
 +
    }
 +
    @Override
 +
    protected void onPostExecute(Boolean aBoolean) {
 +
        super.onPostExecute(aBoolean);
 +
        Log.i(TAG, "onPostExecute");
 +
    }
 +
}
 +
</pre>
 +
 +
ストレージをcachedirとしているがいろいろ変更できる
 +
String storagePath = context.getExternalCacheDir().toString(); // cachedir
 +
 +
参考:http://blog.lciel.jp/blog/2014/02/08/android-about-storage/
 +
 
==参考==
 
==参考==
 
動画のダウンロード
 
動画のダウンロード

2018年11月21日 (水) 12:20時点における版

サンプル

class VideoDownloaderConnection extends AsyncTask<Void, Long, Boolean> {
    private String TAG = "VideoDownloaderConnection";
    private Context context;
    String url = "";
    public VideoDownloaderConnection(Context context) {
        this.context = context;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Boolean doInBackground(Void... params) {
        URL u = null;
        InputStream is = null;
        try {
            u = new URL(url);
            is = u.openStream();
            HttpURLConnection huc = (HttpURLConnection)u.openConnection();
            int size = huc.getContentLength();
            if(huc != null) {
                String fileName = "FILE.mp4";
                String storagePath = context.getExternalCacheDir().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) {
            }
        }
        return true;
    }
    @Override
    protected void onProgressUpdate(Long... values) {
        super.onProgressUpdate(values);
    }
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        Log.i(TAG, "onPostExecute");
    }
}

ストレージをcachedirとしているがいろいろ変更できる

String storagePath = context.getExternalCacheDir().toString(); // cachedir

参考:http://blog.lciel.jp/blog/2014/02/08/android-about-storage/

参考

動画のダウンロード https://blog.engineer.adways.net/entry/2016/09/03/010627

Webview download/cache video https://stackoverflow.com/questions/23876103/webview-download-cache-video

YoutubeDownloadMp4 https://stackoverflow.com/questions/7203047/code-for-download-video-from-youtube-on-java-android