-2

I"m able to Download file from WebView but It doesn't show me the progress bar in Notification panel. But If I go to the Download directory of Ext storage, I can see the file.

How can make Download progress bar visible. Any help would be greatly appreciated.

Here is the code from library to initiate download

webView.setDownloadListener(new DownloadListener() {
                    @Override
                    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

                        request.setMimeType(mimeType);
                        //------------------------COOKIE!!------------------------
                        String cookies = CookieManager.getInstance().getCookie(url);
                        request.addRequestHeader("cookie", cookies);
                        //------------------------COOKIE!!------------------------
                        request.addRequestHeader("User-Agent", userAgent);
                        request.setDescription("Downloading file...");
                        request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
                        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                        dm.enqueue(request);
                        Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
                    }
                });

and my manifest file is

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/icon"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.TransNav">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />


            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".UpdateDialog"
        android:theme="@style/Theme.AppCompat.Dialog" />

    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".UpdateService"
        android:enabled="true"
        android:exported="true" />

</application>

Any help would be largely appreciated. And for the records , Download link are generated from Google drive

and my main activity file starts with:

public class MainActivity extends AppCompatActivity {

boolean doubleBackToExitPressed = false;
private TextView textView;
private BottomBar bottomBar;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • On Which OS you are checking ? below Marshmallow or After it ? – Faraz Ahmed Aug 15 '17 at 14:24
  • 1
    I think your file is downloaded but you are unable to view that file is this the case ? – Faraz Ahmed Aug 15 '17 at 14:56
  • @FarazAhmed yes I noticed that , files are inside Download directory indeed. How should I solve it then. As I also have bluestacks emulater is 4.4.2 version and my device is also same version. But Download progress bar can be seen when I run in bluestacks and is absent if I run through my device. – Sagar Rawal Aug 15 '17 at 15:11
  • @FarazAhmed I have edite my question. Thanks for pointing me in right Direction. – Sagar Rawal Aug 15 '17 at 15:54

1 Answers1

1

If you have already file downloaded then get this file from SDCard or Downloaded directory.

  1. Read file from SDCard

Then open the file in default PDF viewer of the application see below

   public static void openPdfFileAsIntent(Context context, File file) {

    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file), "application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent intent = Intent.createChooser(target, "Open File");
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {

        Toast.makeText(context, "Please add PDF Viewer Application", Toast.LENGTH_LONG).show();
    }
}

Usage:

 openPdfFileAsIntent(MainActivity.this,file);
Faraz Ahmed
  • 1,245
  • 1
  • 14
  • 24
  • Well it is an alternative solution, but Is it not possible to view the Download progress bar just like when I download something from Default Browser. It would really help user to keep track of file , how long will it take to download it. – Sagar Rawal Aug 16 '17 at 12:42
  • You can download a file by using custom downloader which publishes progress. – Faraz Ahmed Aug 16 '17 at 14:09