下载和分享的WhatsApp

问题描述 投票:0回答:1

视频被分享之前,完整的视频获得downloaded.I我用下面的代码下载在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();

下载部分在异步task.The问题实行的是分享到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);
        }
    }

如何解决这个问题呢?

java android
1个回答
0
投票

呼叫shareVideoWhatsApp(value);onPostExecute()

© www.soinside.com 2019 - 2024. All rights reserved.