4

I have a video that plays on my application from a server. I want to make it possible to share the video to WhatsApp when the shared button is clicked.

I understand that I have to download load the video to the device storage before sharing through Intent. Below is my code, when the shared button is click the progress bar keeps loading and nothing else happens, I'm also using FileProvider. How do I go about this?

The getVideoUrl() method below takes in the video url and returns the Uri (file path) after the video has been downloaded

    private Uri getVideoUrl(String fileURL) {
        Uri videoUri = null;
        try {
            File rootFile = new File(getCacheDir(), "share_video_" + System.currentTimeMillis() + ".mp4");
            URL url = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.connect();
            FileOutputStream f = new FileOutputStream(rootFile);
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            videoUri = FileProvider.getUriForFile(this,
                    getPackageName() + ".provider", rootFile);
            f.close();
        } catch (IOException e) {
            Log.d("Error....", e.toString());
        }
        return videoUri; // returns the file path to the video from storage
    }

method to share the video,which is called is onClick of the share button. Then I receive the resultCode in onActivityResult() and make the progressBar invisible and display a message that the video has been shared.

 public void shareVideo(String videoUrl, String desc){
        progressBar.setVisibility(View.VISIBLE);
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("*/*");
        i.setPackage("com.whatsapp");
        i.putExtra(Intent.EXTRA_TEXT, desc );  //to share text
        i.putExtra(Intent.EXTRA_STREAM, getVideoUrl(videoUrl));   //to share video
        i = Intent.createChooser(i, "Share video");
        startActivityForResult(i, POSTED_VIDEO);

    }

shareVideo() is now called in the shareButtonOnClickListner()

@Override
    public void onClick(View v) {
        String videoUrl = "https://linktoVideo.mp4"; //just an example link
        String desc = "Shared Video;
        switch (v.getId()) {
            case R.id.post_image:
                shareVideo(videoUrl, desc);
        }
    }

What am I doing wrongly?

steveOS
  • 125
  • 1
  • 12
  • @blackapps `shareVideo()` is called in onClick and the url to the video and string desc are passed into the method. `getVideoUrl(videoUrl));` is supposed to return the file path of the video after it has been downloaded. – steveOS Aug 01 '20 at 21:21
  • @blackapps I've adjusted the question. Help with any suggestion you have. What am trying to do is share a video to whatsapp, but the video is contained in a url. – steveOS Aug 04 '20 at 08:47
  • `i.putExtra(Intent.EXTRA_STREAM, getVideoUrl(videoUrl))` You cannot call getVideoUrl there as you should call it in a thread. I dont understand you are not getting a `NetworkOnMainThreadException`. – blackapps Aug 04 '20 at 09:18
  • Why dont you share the url? – blackapps Aug 04 '20 at 09:21
  • I understand that I should make such calls on the Main thread, but I am trying to get it to work first. Sharing the url won't serve because the aim is to make user view the video directly on whatsapp. – steveOS Aug 04 '20 at 09:28
  • First download the file and when done build your intent. – blackapps Aug 04 '20 at 09:37
  • I meant shouldn't, that was a typo. Thanks I will keep trying – steveOS Aug 04 '20 at 09:43

0 Answers0