下载并分享Whatsapp

时间:2018-06-03 12:18:47

标签: java android

视频在整个视频下载之前共享。我使用以下代码从服务器下载视频并在whatsapp中分享:

AlertDialog.Builder alert1 = new AlertDialog.Builder(this);
                alert1.setTitle("Share the file to Whatsapp");
                alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        DownloadFile1 downloadFile1 = new DownloadFile1();
                        downloadFile1.videoToDownload = video_url;
                        String value = "test.mp4";
                        downloadFile1.fileName = value;
                        downloadFile1.execute();
                        try {
                            shareVideoWhatsApp(value);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

                alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                    }
                });

                alert1.show();

下载部分是在异步任务中实现的。问题是在下载完整的视频文件之前与whatsapp共享。

下载代码:

class DownloadFile1 extends AsyncTask<String, Integer, String> {
        ProgressDialog bar;
        public String videoToDownload;
        public String fileName;

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        @Override
        protected String doInBackground(String... params) {
            int count;

            try {
                mp4load(videoToDownload);
            } catch (Exception e) {
                // TODO: handle exception
            }

            return null;
        }

        public void mp4load(String urling) {
            try {
                System.out.println("Downloading");
                URL url = new URL(urling);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                //c.setDoOutput(true);
                con.connect();

                // String downloadsPath = Environment.getExternalStoragePublicDirectory();
                File SDCardRoot = Environment.getExternalStorageDirectory();

                File outputFile = new File(SDCardRoot, fileName);

                if (!outputFile.exists()) {
                    outputFile.createNewFile();
                }

                FileOutputStream fos = new FileOutputStream(outputFile);

                int status = con.getResponseCode();

                InputStream is = con.getInputStream();
                int fileLength = con.getContentLength();
                long total = 0;
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                    total += len1;
                    pDialog.setProgress((int) (total * 100 / fileLength));
                }
                fos.close();
                is.close();
                System.out.println("Downloaded");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * Updating progress bar
         */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        /**
         * After completing background task
         * Dismiss the progress dialog
         **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);
        }
    }

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

shareVideoWhatsApp(value);

内拨打onPostExecute()