使用Zxing将Qr结果加载到WebView URL

时间:2019-01-29 10:27:24

标签: android webview

我正在尝试将Qr结果加载到Webview,但仅显示白屏。我做错了什么吗?

这是我的MainActivity     包com.project.ciisapp.ciis;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class ReaderActivity extends AppCompatActivity {
private Button database;
private Button scan_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reader);
    database=(Button)findViewById(R.id.database);
    scan_btn=(Button)findViewById(R.id.scan_btn);


    database.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                startActivity(new Intent(ReaderActivity.this, Main2Activity.class));

        }
    });



    final Activity activity=this;
    scan_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            integrator.setPrompt("Scan");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null){
        if(result.getContents()==null){
            Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
        }
        else {
            Intent intent = new Intent(ReaderActivity.this, scannner.class);
            intent.putExtra("QRurl",resultCode);
            startActivity(intent);
        }
    }
    else {
        super.onActivityResult(requestCode, resultCode, data);
     }
     }


         }

这是我的webviewQR活动,我从其中发送了qr结果的结果

package com.project.ciisapp.ciis;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
 import android.webkit.WebView;


public class scannner extends AppCompatActivity {
 private WebView wvv;



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

wvv = (WebView)findViewById(R.id.wvv);
wvv.setWebChromeClient(new WebChromeClient());
String QRurl = getIntent().getExtras().getString("QRurl");
wvv.loadUrl(QRurl);
WebSettings webSettings = wvv.getSettings();
webSettings.setJavaScriptEnabled(true);

}
    @Override
    public void onBackPressed() {
    if (wvv.canGoBack()){
    wvv.goBack();
    }else {
    super.onBackPressed();
    }
    }
    }

网络视图

package com.project.ciisapp.ciis;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class Main2Activity extends AppCompatActivity {
    private WebView wv;

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

    wv = (WebView)findViewById(R.id.wv);
    wv.setWebChromeClient(new WebChromeClient());
    wv.loadUrl("http://peter42398.000webhostapp.com/");
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);

}

@Override
public void onBackPressed() {
    if (wv.canGoBack()){
        wv.goBack();
    }else {
    super.onBackPressed();
}
 }
 }

扫描仪正在工作,但是用于webview和qr结果用于webview的按钮使我的应用崩溃

1 个答案:

答案 0 :(得分:0)

您将意图结果用作Url。这就是发生此错误的原因。

  

其他活动收到了它,但是它有错误的未知URL方案

您需要像这样在onActivityResult中更改ReaderActivity

       IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if(result != null) {
                if(result.getContents() == null) {
                    toast = "Cancelled from fragment";
                } else {
                    Intent intent = new Intent(ReaderActivity.this, scannner.class);
                    intent.putExtra("QRurl",result.getContents());
                    startActivity(intent);
                }