Android:未找到用于处理Intent的Activity(下载.xml文件)

6ss1mwsb  于 2023-03-06  发布在  Android
关注(0)|答案(1)|浏览(126)

触发包含下载.xml文件的url的下载按钮后崩溃
我是新的Android开发,我想创建一个Android应用程序源从我的网页使用webView,但总是崩溃时,我试图下载.xml文件在模拟器中,有人能帮我纠正这个代码吗?
错误i面

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.webview, PID: 2422
    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=//download:blob:http:mydownloadurl
import ...

public class MainActivity extends AppCompatActivity {
    WebView mWebView;


    String URL = "myWebsite"

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

        mWebView = (WebView) findViewById(R.id.webView);

        //menghidupkan javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(URL);
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.getSettings().setAllowContentAccess(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);

        mWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                Uri uri = Uri.parse("//download:"+url);
                Intent aIntent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(aIntent);
            }
        });

    }

    //method ini perlu ditambahkan agar saat halaman web diload tetap diload di aplikasi, tidak diload di webbrowseer
    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;
        }
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

    }

    //tambahkan method ini agar saat tombol back ditekan, tidak keluar dari aplikasi tapi kembali ke halaman sebelumnya
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
dojqjjoe

dojqjjoe1#

onDownloadStart 中尝试此操作:

Intent aIntent = new Intent(Intent.ACTION_VIEW);
aIntent.setData(Uri.parse(url));
startActivity(aIntent);

如果不起作用,我们可以使用DownloadManager从 onDownloadStart 下的URL下载内容:

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "<<FileName>>");

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();

AndroidManifest.xml 文件中随附此权限:

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

相关问题