1

I just working on a simple App which loads a website in the Webview. this app contains only webview nothing beyond that. Its purpose is to show the webpage as an app.

When my app is opened, the website loads automatically and then navigation flow is working fine.

Here comes the issue, few of my web pages contain download button. On download click, the files with following format ".log , .txt, .pdf " will be downloaded.

When i load website in chrome / Mozilla in android browsers ,the download is working fine. But when I load webpage in the Webview of my app the download is not working.

I connected my phone in debug mode for testing.In the log-cat console I see the below error on every download click in the Webview

12-04 10:47:27.689 7142-7142/com.myweb.mweb W/cr.BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 7142

Below is the MainActivity class

public class MYMainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mymain);

        // clear webview db
        deleteDatabase("webview.db");
        deleteDatabase("webviewCache.db");

        // URL to load
        String url = "https://myweb.com";
        WebView myView = (WebView) findViewById(R.id.mywebview);

        // clear cache
        myView.clearCache(true);
        myView.clearHistory();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookies(null);
        cookieManager.flush();

        WebSettings webSettings = myView.getSettings();
        webSettings.setJavaScriptEnabled(true); // add java script support for webview
        webSettings.setDomStorageEnabled(true);

        // to download the files
        myView.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.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
                intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
                intent.setType("*/*");//any application,any extension
                Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                        Toast.LENGTH_LONG).show();
            }
        });

        // Load only SSL URL and only Authenticated URL
        WebViewClient MyWebViewClient = new WebViewClient()
        {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().endsWith("myweb.com") &&
                        (Uri.parse(url).getScheme().equalsIgnoreCase("https")))  {
                    // This is a trusted site, so do not override; let my WebView load the page
                    return false;
                }
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                // This is by default the Android Browser.
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        };

        // set web client to not redirect and load in the same app opend
        myView.setWebViewClient(MyWebViewClient);
        //myView.setWebViewClient(new WebViewClient());
        myView.loadUrl(url);

    }
}

and this is my Manifest .xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myweb.mweb">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/my_logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MYMainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

why I am unable to download the file via webview.. guys help me out

Rajakrishnan
  • 161
  • 1
  • 4
  • 13
  • because your app can only show these sites, when you download it from chrome app they have done the dunctionality. u need to give permissions and write code for downloading the file – Parth Anjaria Dec 04 '15 at 10:30
  • @ParthAnjaria thanks for quick reply..i am newbie for android & this is my first app, could you guide me with a link where i can find the code and approach which i was missed.. – Rajakrishnan Dec 04 '15 at 10:41
  • http://stackoverflow.com/questions/21864098/how-to-download-files-from-webview-android this might help – Parth Anjaria Dec 04 '15 at 10:51
  • @ParthAnjaria thank you for the link. – Rajakrishnan Dec 04 '15 at 11:01
  • 1
    @Parth Anjaria I found the problem, the download button I used in my site is not a link and it a graphical image triggers the event in ajax. I tried with other sites in my app , code for download in my question is working fine. its my page that's need a code change. thanks for the link which helped me a lot in understanding the download concept. – Rajakrishnan Dec 10 '15 at 13:07

0 Answers0