如何在WebView活动中启用上传文件选项

时间:2019-05-01 19:34:50

标签: android file download android-webview

我想添加促进打开程序的属性,该属性要求用户从文件管理器(包括照相机)中选择一个文件,然后上传所需的更改。还在清单文件中添加了权限。

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,NavigationView.OnNavigationItemSelectedListener{

    public static String PACKAGE_NAME;
    private DrawerLayout drawer;

    private WebView webView;
    private SwipeRefreshLayout swipe;
    private String currentURL = "";

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);
        swipe = findViewById(R.id.swipe);
        swipe.setOnRefreshListener(this);

        // Define url that will open in webview
        String webViewUrl = "http://www.tekies.online";

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setPluginState(PluginState.ON);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setSupportZoom(true);

        if(!isConnected(MainActivity.this)) buildDialog(MainActivity.this).show();
        else {
            webView.loadUrl(webViewUrl);
            startWebView();
        }

//File Download
        webView.setDownloadListener(new DownloadListener(){

            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                DownloadManager.Request myRequest = new DownloadManager.Request(Uri.parse(url));
                myRequest.allowScanningByMediaScanner();
                myRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                DownloadManager myManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
                myManager.enqueue(myRequest);
                Toast.makeText(getApplicationContext(), "File is Downloading...", Toast.LENGTH_SHORT).show();
            }
        });


    }


清单文件权限已添加到应用程序

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

0 个答案:

没有答案