Zxing CaptureActivity.handleDecode() - android

I am new to use Zxing ,When I click a button I want to scan a Two-dimensional code image .This is my MainActivity.java
private Button scan;
scan = (Button) findViewById(R.id.btn_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent, SCAN_CODE);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case SCAN_CODE:
Intent myIntent=getIntent();
Bundle bundle=myIntent.getExtras();
QR=bundle.getString("QR");
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
it will call CaptureActivity.handleDecode(),and This is CaptureActivity.java
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
//I want to get the text in the image.
String result = rawResult.getText();
Intent intent = new Intent();
intent.putExtra("QR", result);
if(result!=null && !"".equals(result))
setResult(RESULT_OK, intent);
else{
setResult(RESULT_CANCELED, intent);
}
finish();
}
But it has exception
enter image description here
and I don't konw why?

You should use data.getStringExtra("QR") in your case, rather than
Intent myIntent = getIntent();
Bundle bundle = myIntent.getExtras();
QR = bundle.getString("QR");
where data is onActivityResult()'s parameter.
And use !result.isEmpty() instead of !"".equals(result).
It's more readable.
Hope it helps you!

Related

onActivityResult() not working on Android

I'm trying to use onActivityResult to send a title, I've looked at the google implementation for the same task but it doesn't work me. Can someone help?
code for onActivityResult in mainActivity.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
System.out.println("There is something coming to this function" + requestCode);
if(requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
Title title = new Title(data.getStringExtra(notesSection.EXTRA_REPLY));
mTitleViewModel.insert(title);
}else{
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
}
}
code in my notesSection activity.
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// System.out.println("Button Has Been Clicked From Notes Section");
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
// System.out.println("Empty?");
setResult(RESULT_CANCELED, replyIntent);
}else{
// System.out.println("Sending Something Supposedly");
String title = titleEdit.getText().toString();
// System.out.println("Sending " + title);
replyIntent.putExtra(EXTRA_REPLY, title);
setResult(RESULT_OK, replyIntent);
}
finish();
// startActivity(new Intent(notesSection.this, MainActivity.class));
}
});
FYI: When I print something in the onActivityResult function, nothing shows up on my run terminal, I don't know why this is, but I don't think the function is being reached for some reason. I will send more code if necessary.
Thanks.
In your first activity try starting second activity like this
static int NEW_TITLE_ACTIVITY_REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
---
---
---
// i assume you are starting activity on some button click
// so add following line in you button on click event
startActivityForResult(new Intent(MainActivity.this,notesSection.class),NEW_TITLE_ACTIVITY_REQUEST_CODE);
}
then override following method in your FIRST ACTIVITY
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE)
{
// do whatever you want
}
}
Also update your finish fab on click listener
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
setResult(RESULT_CANCELED, replyIntent);
}
else{
replyIntent.putExtra("TITLE", titleEdit.getText().toString());
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
Actually, this procedure has changed recently. You might want to take a look at the official documentation so that you can implement it according to your needs.

startActivityForResult doesn't work in a Fragment while image upload from camera or gallery

public class Profile extends Fragment implements Profile_frg{
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog d = new Dialog(mainActivity);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.activity_custom_dialog);
d.setCanceledOnTouchOutside(true);
gallery = (ImageView) d.findViewById(R.id.imageView1);
camera = (ImageView) d.findViewById(R.id.imageView2);
cancel = (ImageView) d.findViewById(R.id.imageView3);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.dismiss();
}
});
gallery.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
gintent, "Select Picture"), PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(mainActivity,
e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
d.dismiss();
}
});
camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// define the file-name to save photo taken by Camera
// activity
String fileName = "new-photo-name.jpg";
// create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image captured by camera");
// imageUri is the current activity attribute, define
// and save it for later usage (also in
// onSaveInstanceState)
imageUri = context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
// create new Intent
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, PICK_Camera_IMAGE);
d.dismiss();
}
});
d.show();
}
});
}// Work Fine till here...
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}//didn't detect this method
Yes, there is no onActivityResult() callback in fragments.
You have to override activityResult method in your host activity(in which your fragment is defined)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == GALLERY/CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Fragment yourFragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG"); // same tag while adding fragment for the first time.
if (yourFragment != null) {
yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
}
}
super.onActivityResult(requestCode, resultCode, data);
}
And in your fragment do like this :
public void onActivityResult(int requestCode,int resultCode,Intent data) {
...
Pull your image data from data object
do your further process from here.
...
}
Yes, startActivityForResult will not work directly if you are calling it from any fragment. After getting the result, the callback will hit the onActivityResult of the Hosting Activity from where you have to manually redirect it to the respective fragment.
Below is the sample code of the onActivityResult of your Activity. Please note that this only redirect the result to the respective fragment.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1001:
Fragment frag = getSupportFragmentManager().findFragmentByTag("TAG"); // TAG should be same as the one you entered while adding the fragment
if (frag != null) {
frag .onActivityResult(requestCode, resultCode, data);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

how to receive data in previous activity in Android

I want to receive data in previous Activity from next activity like (1 <--- 2 ). I tried but data is not received from second to first activity .
This is First Cativity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("ActivityId", getItemActivity);
i.putExtra("Vessel", strVesselName);
i.putExtra("HashTag", strHashTag);
i.putExtra("RemarkTitle", strRemark);
i.putExtra("ShortRGN", strShortTypeRGN);
i.putExtra("VessId", strvesselid);
startActivity(i);
This is second Activity
Intent intent = getIntent();
strActivityId = intent.getStringExtra("ActivityId");
strVesselName = intent.getStringExtra("Vessel");
strHashTag = intent.getStringExtra("HashTag");
strRemark = intent.getStringExtra("RemarkTitle");
strShortRGN = intent.getStringExtra("ShortRGN");
strVessId = intent.getStringExtra("VessId");
img_AddPostAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Edit_Post.this, EditRecord.class);
i.putExtra("EditVesselId", strVessId);
i.putExtra("EditActivityId" , strActivityId);
startActivity(i);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String audioString=data.getStringExtra("AUDIO_STRING");
Log.e(" audioString "," = "+audioString);
}
}
This is Third Activity
Intent intent = getIntent();
vesselId = intent.getStringExtra("EditVesselId");
strActivityId = intent.getStringExtra("EditActivityId");
Intent intent=new Intent(EditRecord.this, Edit_Post.class);
intent.putExtra("AUDIO_STRING",newAudioFile);
setResult(REQUEST_CODE, intent);
finish();
do this when you are calling the second activity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("HashTag", strHashTag);
startActivityForResult(i, REQUEST_CODE);
Now you need to set the result what you want on CustomActionActivity
e.g.
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(REQUEST_CODE,intent);
finish();
Now you will get this data to the your first activity
e.g.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
let me know in case of any issue
Use SharedPreferences
Store the data in a SharedPreference and access it in from the SharedPreference in the other activity.
Add the data to the SharedPreference in the onStop for the 2nd Activity and access it in the onCreate of the other Activity
#Override
public void onStop(){//Where you wish to insert data
SharedPreferences data=getSharedPreferences(PREFS_FILE,0);
SharedPreferences.Editor editor= count.edit();
editor.put("data","DATA");
editor.apply();
super.onStop();
}
in onCreate() of the other Activity:
SharedPreferences data = getApplicationContext().getSharedPreferences(PREFS_FILE, 0);
String dataString=data.get("Data",0);
Hope this helps. Cheers.
I am doing this way its works for me:
Activity 2:
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("MESSAGE", strtext + "");
setResult(2, intent);
if (isclose) {
finish();
} else {
super.onBackPressed();
}
}
}
Activity1:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String sSuName = data.getStringExtra("MESSAGE");
//txtfavouratecount.setText(sSuName);
}
in Your onclick listener
Intent itemintent = new Intent(context,your target Activity.class);
Bundle b = new Bundle();
b.putStringArray("iarray", Qtag);
b.putInt("mflag", 0);
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivityForResult(itemintent,2);
I would do something like this:
In Activity A:
private static final int REQUEST_CODE = 9001;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE);
In Activity B:
Intent data = new Intent();
data.putExtra("key", parameter);
setResult(CommonStatusCodes.SUCCESS, data);
finish();
And finally, in the Activity A, receive result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
String result = data.getStringExtra("key");
}
} else {
//Error to receive data
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Good luck!

Using StartActiviytForResult

I have Three Activity A,B,C.
From A activity I send it to B using startActivityforResult and then from activity B I Send it to Activity C using startActivityforResult but when I came back to activity A from all activities with RESULTOK than i got no data on activity A and every thing refresh on bluestack but its working fine on Samsung and Motorola as I had tested on that.
So I want it to be working on all devices.
I had set my activity orientation to Portait statically in all activities.
Code:
Acitivity A:
Intent i = new Intent(v.getContext(),B.class);
startActivityForResult(i, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
String result = data.getStringExtra("imagesreturn");
}
}
}
Activity B:
ImageButton btn = (ImageButton)findViewById(R.id.button_capture);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(CameraUiActivity.this, CameraImageTakenActivity.class);
i.putExtra("image", f.getAbsolutePath());
startActivityForResult(i, 1);
// dialog.dismiss();
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("images");
Intent intent = new Intent();
intent.putExtra("imagesreturn",result);
setResult(RESULT_OK, intent);
finish();
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
finish();
}
}
}
Activity C:
final String myimage = b.getString("image");
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("images",myimage);
setResult(RESULT_OK, intent);
finish();
}
});

pass result form current activity to first activity when go back android

I have two classes NoteEdit and NoteView, in NoteView class I press edit button it direct me to NoteEidt class, suppose I want to pass my updated body information to NoteView, and the previous body information should have been updated in NoteView interface, but it still remain the same, this is part of my code, please help, thanks!
NoteEdit class:
Intent resultIntent = new Intent(NoteEdit.this, NoteView.class);
String body = mBodyText.getText().toString()
resultIntent.putExtra(body, true);
NoteView class
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DIARY_EDIT:
Intent i = new Intent(NoteView.this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
startActivityForResult(i, ACTIVITY_VIEWERS);
break;
} }
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case (ACTIVITY_VIEWERS)
: {
if (resultCode == Activity.RESULT_OK)
{
Bundle extras = getIntent().getExtras();
newText =extras.getString(NotesDbAdapter.KEY_BODY);
// TODO Update your TextView.
}
break;
}
}
}
now use this code.
NoteEdit class:
Intent resultIntent = new Intent(NoteEdit.this, NoteView.class);
String body = mBodyText.getText().toString()
resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body);
setResult(RESULT_OK,resultIntent);
NoteView class:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DIARY_EDIT:
Intent i = new Intent(NoteView.this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
startActivityForResult(i, ACTIVITY_VIEWERS);
break;
} }
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case ACTIVITY_VIEWERS
: {
if (resultCode == RESULT_OK)
{
Bundle extras = intent().getExtras();
newText =extras.getString(NotesDbAdapter.KEY_BODY);
// TODO Update your TextView.
}
break;
}
}
}
use setResult(Activity.RESULT_OK, resultIntent) in NodeEdit
and see http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Edit
also you should use resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body); in NodeEdit to pass String value body to another activity. and get it by using extras.getString(NotesDbAdapter.KEY_BODY);
try something like this.
Intent resultIntent = getIntent();
String body = mBodyText.getText().toString()
resultIntent.putExtra(body, true);
setResult(RESULT_OK,resultIntent);
finish();
dont forget to put a call to finish().
NoteEdit class:
//write this code when edit finished
Intent resultIntent = new Intent(getIntent()); // Note this line is important!
String body = mBodyText.getText().toString()
resultIntent.putExtra(NotesDbAdapter.KEY_BODY, body);
setResult(RESULT_OK,resultIntent);
finish();
NoteView class:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DIARY_EDIT:
Intent i = new Intent(NoteView.this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, mRowId);
startActivityForResult(i, ACTIVITY_VIEWERS);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case ACTIVITY_VIEWERS: {
if (resultCode == RESULT_OK) {
Bundle extras = intent().getExtras();
newText =extras.getString(NotesDbAdapter.KEY_BODY);
// TODO Update your TextView.
}
break;
}
}
}

Categories

Resources