I want the user to choose a QR reader from his installed apps. This could be done by using the Intent.createChooser. When a picture is taken with the QR reader, the QR code should be sent back to my application. This is what Ive tried so far:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
String title = (String) getResources().getText(R.string.chooser_title);
Intent chooser = Intent.createChooser(intent, title);
startActivityForResult(chooser, CUSTOM_REQUEST_QR_SCANNER);
The scanner doens't start correctly, it only shows an sample QR code. I have a feeling the intent.setType("text/plain") might be wrong? What type is a QR reader? Or how do i start a QR reader this way correctly?
I also have an ActivityResult when the QR app is done:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == CUSTOM_REQUEST_QR_SCANNER) {
Log.d(TAG, "QR activity complete");
//Successful scan
if (resultCode == RESULT_OK) {
Replace
intent.setType("text/plain");
with
intent.setType("com.google.zxing.client.android.SCAN");
Follow This Demo and do include "android-integration.jar" in your project it has this .jar file also... and also you can download Zxing Library from Here it will use the available QR Code Scanner in your app. There are also other ways just use this first you will get to know other also by R and D.
OR
Use this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_scan =(Button) findViewById(R.id.btn_scan);
btn_scan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (result != null) {
String contents = result.getContents();
if (contents != null) {
showDialog("Found QRcode", result.toString());
} else {
showDialog("NO QRcode Found", "The user gave up and pressed Back");
}
}
}
private void showDialog(String title, CharSequence message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", null);
builder.show();
}
and include same .jar file in project properties java build path. you can download .jar from here same link.
Related
I wrote an android application which loads a form in webview and it allows user to scan a barcode and copy the result of the barcode in the clipboard. I want to paste that from clipboard to the webview using android's paste function. When I try to search for paste it just shows me to get the data from clipboard and put it in an EditText field, But that's not what I want. I want to use the stock paste function (similar to longpress the field -> paste) in my code. Thanks
public void clickScan(View view){
final Activity activity = this;
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan the serial number");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
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(getApplicationContext(), "Error Scanning the code. Please try again!", Toast.LENGTH_LONG).show();
}
else {
String input = result.getContents().toString();
((ClipboardManager)getSystemService(getApplicationContext().CLIPBOARD_SERVICE)).setText(input);
Toast.makeText(getApplicationContext(), input,Toast.LENGTH_LONG).show();
}
}
else{
super.onActivityResult(requestCode, resultCode, data);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = "test";
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://docs.google.com/forms/d/e/1FAIpQLSesTx1IBlPZlN5RXzzauJWYxStHOqt7wH_z4lFe0JHQmKm91w/viewform?usp=sf_link");
}
}
Found an answer guys.
I used:-
webView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_PASTE));
Thanks.
I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?
start intent:
Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);
Closing intent
public void closeButton(View view){
finish();
}
Any help would be appreciated! thanks!
I assume you are using your own FileChoser class, not a standard Android one:
private static final int FileChooserRequestCode = 666;
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FillChooserRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// ... file is chosen
String fileName = data.getStringExtra("FileName");
} else {
... dialog is closed
}
}
}
in FileChoser you do
Intent intent = new Intent();
intent.putStringExtra("FileName", fileName);
SetResult(Activity.RESULT_OK, intent);
finish();
and
SetResult(Activity.RESULT_CANCELED);
finish();
You can use startActivityForResult() please refer the link Getting Results From Activity
static final int FILE_CHOOSER_INTENT = 1; // The request code
...
private void chooseFile() {
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FILE_CHOOSER_INTENT);
}
Call setResult pass your result data as Intent. for details refer link SetResult function
Override this in your calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == FILE_CHOOSER_INTENT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
I have the following scenario :
I used the Maven repository from Gradle to integrate ZXing into my Android app.
In my scan activity, the code looks like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_layout);
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt(" ");
integrator.setScanningRectangle(700, 700);
integrator.setResultDisplayDuration(0);
integrator.setCameraId(0); // Use a specific camera of the device
integrator.initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = null;
scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
if(isConnected()) {
requestdata("http://rm360project-001-site1.smarterasp.net/api/endpoint", scanContent);
}else {
Toast.makeText(this, "Internet Connection not available", Toast.LENGTH_LONG).show();
}
} else {
Intent getMainScreen = new Intent(ScanScreen.this, MainActivity.class);//pentru test, de sters
startActivity(getMainScreen);
}
}
The way I want it to work :
1. If I scan a QR code, call the function requestdata
2. If I press back during scan, go to MainActivity
The problem :
Even when I press back on my device, the function requestdata is called, I think because scaningResult is never null. Shouldn't it be null when back is pressed?
Do you have any ideea why this happens?
Thank you!
Don't know if your still interested but...
Simply change this line:
if (scanningResult != null) {
To this:
if (scanningResult != null && resultCode==RESULT_OK) {
For some reason simply scanningResult does not actually return null as suggested by the ZXing team, even when the Intent is canceled.
I have an app, i used this code to integrate zxing
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
I have both zxing scanner as well as google goggles installed in my mobile phone. When i start the app and try to scan, I get the option to choose either the barcode scanner or the goggle app. I thought, hey let's try and use the goggle app for doing other stuff as well like OCR. I select the goggle option but the app does not have the take picture option within it. How do I integrate goggles also with my app? with full functionality?
I have no idea about how to integrate your app with Google Goggles. However, if you looking for an app that provide OCR function, you may use my app:
https://play.google.com/store/apps/details?id=sunbulmh.ocr
Here is a sample code that you can use in your app to get OCR service:
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo("sunbulmh.ocr", PackageManager.GET_ACTIVITIES);
Intent LaunchIntent = pm.getLaunchIntentForPackage("sunbulmh.ocr");
LaunchIntent.setFlags(0);
startActivityForResult(LaunchIntent,5);
} catch (NameNotFoundException e) {
Uri URLURI = Uri.parse("http://play.google.com/store/apps/details?id=sunbulmh.ocr");
Intent intent = new Intent(Intent.ACTION_VIEW,URLURI);
startActivity(intent);
}
Then, get the result in onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == 5){
String ocr_txt = data.getStringExtra(Intent.EXTRA_TEXT);
// ocr_txt contains the recognized text.
}
}
}
I am new to Android programming and I'm writing an application in Java that opens the camera take a photo and save it. I made it via Intents but I can't see onActivityResult running.
I have tested it into my phone (Samsung Galaxy S) and when I take the photo I receive a preview of that photo having two buttons one Save and the other Cancel. I haven't added something to my code to do this so I think it's something that camera does. I want after capturing the image to run onActivityResult (after I press the Save button on the preview).
But how I'm going to return a result to start onActivityResult after pressing the Button Save on the preview?
I FORGOT to tell that after i press save my entire app is terminated.
Here is my Code
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
try the below code, you will have to modify it a bit, it will help you get From Library and From Camera both, the SELECT_PICTURE is used for getting image from library
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null)
myFile = new File(selectedImagePath);
else if (filemanagerstring != null)
myFile = new File(filemanagerstring);
if (myFile != null) {
Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);
break;
case CAMERA_REQUEST:
Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");
break;
default:
break;
}
}