0

I have managed to download my video on the device, but cannot access it again to play it on my app. I am using the generated uri to reference my video back to the videoView, but the uri generated at

uri= downloadManager.getUriForDownloadedFile(reference); is null according my my log message.

What am I doing wrong?

public void saveLocal(View v){

    downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    String url= "http://adme360.otscom.net/tim10.mp4";
    uris= Uri.parse(url);
    DownloadManager.Request request= new DownloadManager.Request(uris);
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Long reference=downloadManager.enqueue(request);
    Toast.makeText(this,""+reference,Toast.LENGTH_LONG);



    uri= downloadManager.getUriForDownloadedFile(reference);

    Log.d("REFERENCE", ""+uri);
}
public void playLocal(View v){
    video.setVideoURI(uri);
    video.start();

}
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Naman Jain
  • 321
  • 5
  • 21

1 Answers1

0

The download is down asynchronously. The video is downloaded on an other thread. You need to register a broadcast receiver to get the callback when your download is completed. Quoting from answer from this question

A Broadcast intent action sent by the download manager when a download completes so you need to register a receiver for when the download is complete:

To register receiver

registerReceiver(onComplete, newIntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

and a BroadcastReciever handler

BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
    // your code
    }
};`
Community
  • 1
  • 1
karandeep singh
  • 2,294
  • 1
  • 15
  • 22
  • Your answer makes sense but is not working for me. What I am trying to do is download the file, find its unique Long reference, and then try and play the video by using the find Uri. My Uri value is returning null. I want to know why, because my download is very successful. – Naman Jain Feb 08 '18 at 19:11
  • you need to write that code in onReceive of broadcaseReciever – karandeep singh Feb 08 '18 at 19:14
  • Yeah. I tried it out. It works. Your right, the video download occurs in a separate thread. – Naman Jain Feb 08 '18 at 19:46