Bhavikkumar
Bhavikkumar

Reputation: 446

How I can download video in my application like we download or install apk file on google play?

I want to download video from URL in background like we install apk file in background from Google play. I want to show downloading indicator on notification bar same like it showing while we download or install apk file. if any suggestion or idea then suggest me how I can achieve this task in my project.

Upvotes: 0

Views: 380

Answers (2)

Dhaval Solanki
Dhaval Solanki

Reputation: 4705

Use Download Manager,

private void for_call_download() {
        File folder = Environment.getExternalStoragePublicDirectory(DOWNLOAD_FOLDER_NAME);
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdirs();
        }
        try {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(APK_URL));
            request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, DOWNLOAD_FILE_NAME);
            request.setTitle(SessionName);
            request.setDescription("" + SessionDesc);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setVisibleInDownloadsUi(false);
            request.setMimeType("application/cn.trinea.download.file");
            downloadId = downloadManager.enqueue(request);
        } catch (IllegalStateException i) {
            showAlert(context, "Sorry Some Problem");
            i.printStackTrace();
        }
        updateView();
        status_download = true;
    }

Upvotes: 0

Pravin
Pravin

Reputation: 1362

You can prefer android DownLoadManager.
somehow want to do like this in your activity

String mUrl="your video url";

DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(mUrl));
            request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, your_fileName);
            request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
            request.setAllowedOverRoaming(false);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
            manager.enqueue(request);


setNotificationVisibility() -> show download notification in notification bar(window) with download progress also.

below are three methods where you can choose destination for your downloaded file path.
1. setDestinationInExternalFilesDir(Context context, String dirType, String subPath) Set the local destination for the downloaded file to a path within the application's external files directory (as returned by getExternalFilesDir(String).


2. setDestinationInExternalPublicDir(String dirType, String subPath) Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).


3. setDestinationUri(Uri uri) Set the local destination for the downloaded file.

Upvotes: 2

Related Questions