如何使用AsyncTask下载大型视频文件?

时间:2019-05-10 05:48:37

标签: android android-asynctask video-streaming android-videoview

能不能有人解释我在做什么,我正在开发一个需要下载视频并将文件保存到设备上然后播放视频的应用程序。 我正在用图像和视频测试应用程序,可以下载图像并将其保存在设备上,也可以下载视频然后播放视频.....问题是,如果我关闭应用程序然后我尝试播放视频并收到错误“无内容提供商” 但是图像在那里,所以看起来无法将图像保存在设备上,但是不能将视频保存在设备上?

任何想法为什么会发生这种情况。

public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();

//        String image_url = "https://st1.uvnimg.com/dims4/default/48609b9/2147483647/resize/1093x820%3E/quality/75/?url=https%3A%2F%2Fuvn-brightspot.s3.amazonaws.com%2F95%2F69%2F0a7817d84e678beb6ffbc7c14f3e%2Fresizes%2F1500%2Fgettyimages-1019549260.jpg";
//    String image_url = "https://st1.uvnimg.com/dims4/default/2cf8c08/2147483647/resize/935x645%3E/quality/75/?url=https%3A%2F%2Fuvn-brightspot.s3.amazonaws.com%2Ffd%2F77%2F07f78fb84b35aed6eda04734d014%2Fresizes%2F1500%2F20190509-3586.jpg";
        String image_url = "https://mywebsize.com/testvido.mp4";
    Button button;
    Button button2;
    Button button3;
    Button button4;
    VideoView videoView;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        imageView = (ImageView) findViewById(R.id.image_view);
        videoView = (VideoView) findViewById(R.id.videoview);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadTask downloadTask = new DownloadTask();
                downloadTask.execute(image_url);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = "sdcard/photoalbum/image.jpg";
                Log.e(TAG, "PATh:" + path);
                imageView.setImageDrawable(Drawable.createFromPath(path));
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = "sdcard/photoalbum/image2.jpg";
                Log.e(TAG, "PATh:" + path);
                imageView.setImageDrawable(Drawable.createFromPath(path));
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = "sdcard/photoalbum/video.mp4";
                Log.e(TAG, "PATh:" + path);
                videoView.setVideoURI(Uri.parse(path));
            }
        });
    }

    class DownloadTask extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            String path = params[0];
            int file_length = 0;

            try {
                URL url = new URL(path);
                URLConnection urlConnection = url.openConnection();
                urlConnection.connect();
                file_length = urlConnection.getContentLength();
                File new_folder = new File("sdcard/photoalbum");
                Log.e(TAG, "PATh:" + new_folder);
                if(!new_folder.exists()) {
                    new_folder.mkdir();
                }

                File input_file = new File(new_folder, "video.mp4");
                Log.e(TAG, "PATh:" + input_file);
                InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);
                byte[] data = new byte[1027];
                int total = 0;
                int count = 0;
                OutputStream outputStream = new FileOutputStream(input_file);
                while ((count=inputStream.read(data)) != -1) {
                    total += count;
                    outputStream.write(data, 0, count);
                    Log.e(TAG, "Descargando: "+ total);

                }

                inputStream.close();
                outputStream.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return "Download complete";
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
            String path = "sdcard/photoalbum/video.mp4";
            Log.e(TAG, "PATh:" + path);
//            imageView.setImageDrawable(Drawable.createFromPath(path));
            videoView.setVideoURI(Uri.parse(path));
            videoView.start();
        }

    }
}

0 个答案:

没有答案