Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Whatsapp中下载和共享_Java_Android - Fatal编程技术网

Java 在Whatsapp中下载和共享

Java 在Whatsapp中下载和共享,java,android,Java,Android,在下载完整视频之前共享视频。我使用以下代码从服务器下载视频并在whatsapp中共享: AlertDialog.Builder alert1 = new AlertDialog.Builder(this); alert1.setTitle("Share the file to Whatsapp"); alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener(

在下载完整视频之前共享视频。我使用以下代码从服务器下载视频并在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扩展异步任务{
进程对话框栏;
公共字符串videoToDownload;
公共字符串文件名;
/**
*在启动后台线程之前
*显示进度条对话框
*/
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
整数计数;
试一试{
mp4load(视频下载);
}捕获(例外e){
//TODO:处理异常
}
返回null;
}
公共void mp4load(字符串URL化){
试一试{
System.out.println(“下载”);
URL=新URL(URL链接);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“GET”);
//c、 设置输出(真);
con.connect();
//字符串downloadsPath=Environment.getExternalStoragePublicDirectory();
文件SDCardRoot=Environment.getExternalStorageDirectory();
File outputFile=新文件(SDCardRoot,文件名);
如果(!outputFile.exists()){
outputFile.createNewFile();
}
FileOutputStream fos=新的FileOutputStream(outputFile);
int status=con.getResponseCode();
InputStream=con.getInputStream();
int fileLength=con.getContentLength();
长总计=0;
字节[]缓冲区=新字节[1024];
int len1=0;
而((len1=is.read(buffer))!=-1){
fos.写入(缓冲区,0,len1);
总+=len1;
pDialog.setProgress((int)(总计*100/文件长度));
}
fos.close();
is.close();
System.out.println(“下载”);
}捕获(IOE异常){
e、 printStackTrace();
}
}
/**
*更新进度条
*/
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后
*关闭进度对话框
**/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载文件后关闭对话框
解雇对话框(进度条类型);
}
}

如何解决此问题?

调用
共享视频WhatsApp(值)内部
onPostExecute()

调用
shareVideoWhatsApp(值)内部
onPostExecute()