Reputation:
I have already created a QR scanner android application. What I need is that if I scan a QR code and if it generates a link, so the app should automatically open that link through the browser. Any help will be appreciated. Thank You!!
Upvotes: 0
Views: 2011
Reputation:
Down below is my QR scanner code :
public class VehicleReaderActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("SCAN QR CODE");
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
// Do anything with result here
Log.w("handleResult", result.getText());
// Now direct the generated link to the browser //
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + result.getText()));
startActivity(browserIntent);
//Resume scanning
//mScannerView.resumeCameraPreview(this);
}
}
Upvotes: 0
Reputation: 321
If you have already implemented a "QR scanner" then check, there must be a callback method where you receive the returned text from the "Scanner".
Inside that callback method, fire an intent to open the browser with a specific url like shown below:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url)); // here ulr is the one that you get from scanner
startActivity(i);
I hope it helps.
Upvotes: 0
Reputation: 3283
When you get the callback of QR code generated
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
To open that link in browser
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
To validate the URL
URLUtil.isValidUrl(url)
Upvotes: 1