How to combine insert code and bar code code together in android? - android

I have two sets of code. Both sets require the same name onActivityResult but I'm not sure how to combine them without clashing.
The two sets of code are:
1st Set: Users are required to click on the no-image ImageView to select an image from their mobile device and then it will display the image that they have selected.
2nd Set: Users are required to click a Button to scan a barcode. It will open a camera for the user to scan the barcode and after scanning, it will display the number from the barcode.
Here is the code for two sets:
1st Set of Code:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check for Image Request , If user get the image is ok, and the image is not null
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
//Put the image data into this mImageUri
mImageUri = data.getData();
//This line take the data from mImageUri and load into mImageView so basically changing the uri to image
Picasso.with(this).load(mImageUri).into(ItemORFoodImg);
}
}
2nd Set of Code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
final IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if(result != null){
if(result.getContents() == null){
Toast.makeText(this, "Result Not Found", Toast.LENGTH_SHORT).show();
}
else{
AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(this);
alertdialogbuilder.setMessage(result.getContents()+"\n\nWant to add as new item?");
alertdialogbuilder.setTitle("ResultScanned");
alertdialogbuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String barcode = result.getContents();
Intent intent = new Intent(BarcodeTest.this,AddNewItem.class);
intent.putExtra(BarID, barcode);
startActivity(intent);
}
});
alertdialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog alertDialog = alertdialogbuilder.create();
alertDialog.show();
}
}
else {
super.onActivityResult(requestCode,resultCode,data);
}
}
Can someone help me combine these two code snippets without clashing?

You can use IntentService.
You can create two java class one for insert code and one for QR code.
When pressed ImageView go to insert code class and when pressed button go to QR code class.
And then pass data to your Activity.
Follow this link
https://developer.android.com/reference/android/app/IntentService

Related

Freeze activity after Dialog prompt on onActivityResult function

I want my application to continue accepting image from the gallery if I chose Upload Image from the dialog box, but the problem is that the function will continue to finish (the log for count is printed even if I didn't pressed any button, and the conditional statement will set cont = false). The decision variable is a global string variable.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
boolean cont = true;
while (cont == true) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
functionHere();
}
if (requestCode == OPEN_DOCUMENT_CODE && resultCode == RESULT_OK) {
if (data != null) {
// this is the image selected by the user
try {
functionHere();
} catch (Exception ex) {
Log.i("Error", ex.toString());
}
}
}
continuePrompt();
if(decision == "Upload"){
requestCode = OPEN_DOCUMENT_CODE;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, OPEN_DOCUMENT_CODE);
}
else if(decision == "Take Picture"){
//code here
}
else if(decision == "End"){
cont = false;
}
else{
cont = false;
}
Log.d("Count", Integer.toString(count));
}
if (cont == false) {
//output result
}
}
Here is my code for my dialog which I get from another question here in stackoverflow
public void continuePrompt() {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Platelet detection");
builder.setMessage("Are all microscopic slide image uploaded?");
// add the buttons
builder.setPositiveButton("Upload Image", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
decision = "Upload";
}
});
builder.setNeutralButton("Take Picture", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
decision = "Take Picture";
}
});
builder.setNegativeButton("End", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
decision = "End";
}
});
// create and show the alert dialog
builder.show();
}
you still have below line in your code
while (cont == true)
even when you show your dialog this loop is iterating over and over again. you should fix your logic, there shouldn't be any while loop, everything is already in UI thread and with above line you are hanging it showing dialogs one after another. you should show your prompt once and instead of setting global decision variable just place your code response for action in listener
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
functionHere();
continuePrompt();
}
else if (requestCode == OPEN_DOCUMENT_CODE && resultCode == RESULT_OK) {
if (data != null) {
// this is the image selected by the user
try {
functionHere();
continuePrompt();
} catch (Exception ex) {
Log.i("Error", ex.toString());
}
}
}
else{
super.onActivityResult(requestCode, resultCode, data);
}
}
private void continueFlow(){
if("Upload".equals(decision)){
requestCode = OPEN_DOCUMENT_CODE;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, OPEN_DOCUMENT_CODE);
}
else if("Take Picture".equals(decision)){
//code here
}
}
just call continueFlow(); after every decision change (in every listener). or move code form if("Upload".equals(decision)){ straight to setPositiveButton and so on
btw. decision == "Upload" won't ever be true as == operator is comparing same objects. decision is already declared variable and "Upload" String is freshly created in if statement, its brand new variable. for comparing content of Strings (same text) use stringOne.equals(stringTwo);

Passing extra data in startActivityForResult is received null in onActivityResult from another class

I am new to Android development and I am working on a personal project which uses google map and image loading, among others.
I cannot figure out why, when using startActivityForResult combined with onActivityResult from 2 different classes of the same package it does not parse extra data inserted with putExtra
Code:
package com.example.gui;
class InitialMapLoad {
...
lGMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
final LatLng mClickPos = latLng;
ImageView image = new ImageView(lActivity);
image.setImageResource(R.drawable.pin_on_map);
AlertDialog.Builder imgSelectPopup = new AlertDialog.Builder(lActivity);
imgSelectPopup.setTitle(R.string.addImageCoords);
imgSelectPopup.setNegativeButton(R.string.cancelAction, null);
imgSelectPopup.setPositiveButton(R.string.btnOpenGallery, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickPhoto.putExtra("gpsLng", String.valueOf(mClickPos.longitude));
lActivity.startActivityForResult(pickPhoto , LOAD_IMG_REQUEST);
}
});
}
and the main activity class
package com.example.gui;
public class gui extends AppCompatActivity {
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (requestCode == LOAD_IMG_REQUEST) {
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
Log.e(TAG, "onActivityResult: " + selectedImage ); // this is OK
Log.e(TAG, "onActivityResult: " + imageReturnedIntent.getStringExtra("gpsLng") ); // this is null
}
}
}
}
As noted in the comments, the getStringExtra("gpsLng") is null.
Am I missing something? Is there another recommended way to do this?
Thank you in advance!

How to get the result of the qr code to the dialog box then will open to the another activity?

I have an app that will scan Qr code. The Qr code have a default value that if you scan it will go to a dialog box with the result and will call the activity with the same result
I want to happen is when the result of the qr scan is in the dialog box if the result of it is the same with the activity it will open the activity if not it will call a wrong qr.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//We will get scan results here
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
//check for null
if (result != null) {
if (result.getContents() == null) {
// Toast.makeText(this, "Scan Cancelled", Toast.LENGTH_LONG).show();
} else {
//show dialogue with result
showResultDialogue(result.getContents());
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
//method to construct dialogue with scan results
public void showResultDialogue(final String result) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
builder.setTitle("Scan Result")
.setMessage(result)
.setPositiveButton("View your Location", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Scan Result", result);
clipboard.setPrimaryClip(clip);
Intent nxt = new Intent(MainActivity.this, Acad1.class);
startActivity(nxt);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.dismiss();
}
})
.show();
}
No. What i want is when you scan a qr code with the same name with activity it will open first the dialog box and then when you click the view location it will go to the activity with the same name of the result.

Activity recreating before getting a resultant url after capturing a picture In redmi Note 4

I have one activity and multiple fragments.Here my use case is I have to capture an image using from one of the fragment,the resultant image will shown in an ImageView in that fragment.But here the problem is the fragment destroying its view and activity was recreating again.How can I show the resultant image in that fragment ? This problem occuring only in RedmiNote4.Thanks in advance.
Here is my code
cameraBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//check permissions
if(hasPermission){
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
mCapturedImageURI =getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
intentPicture.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if(intentPicture.resolveActivity(getActivity().getApplicationContext().getPackageManager()) != null) {
startActivityForResult(intentPicture,CAMERA_REQUEST);
}
}
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
} else if(requestCode == CAMERA_REQUEST ){
Uri uri =mCapturedImageURI;
//setImage
loadImage(uri)
}
}
I am also RedmiNote4 user.
With this case I was facing that issue.
When I turn off flag "Don't keep activities" It solved my issue after digging solution for 2 days.

How to show result that has been stored in a string variable in a dialog in android

I am trying to scan QR code with xzing import and show the QR details in the dialog box with 2 buttons.First button should be able to make a call to a fixed number(Emergency Number) and the second button should be able to share QR details along with my current location through whatsapp.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
get the extras that are returned from the intent
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
>I want to show the string value in the dialogbox with 2 buttons.
showDialog(this);
}
}
}
private void showDialog(AndroidQrCodeExample androidQrCodeExample) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
>I dont know whether I can use this command setmessage to display as I am new in android.Please explain me How to do it.
.setMessage(contents)
.setTitle("Response")
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
builder.show();
}
}strong text

Categories

Resources