I'm using the zxing library to provide qrcodes and scanning to an application however I'm stuck on an issue. When attempting to scan, I've tried several methods. The recommended:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
or the Intent method:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
this.startActivityForResult(intent, 0);
Both appear to work fine as the Scan app or choose the app to handle the intent dialog appears. However, after scanning, the app immediately closes. No errors and no results. Just bye-bye.
method to catch the result, note the Log.d immediately after super() is never displayed.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(LOG_TAG, "resultcode = " + resultCode);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, data);
if (scanResult == null) {
return;
}
final String result = scanResult.getContents(); // Your result
if (result != null) {
Log.d(LOG_TAG, "Your result is: " + result);
}
break;
default:
}
}
I've seen references in some tutorials explaining how to implement zxing make entries in the AndroidManifest.xml. Seen below:
<activity android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
I'm just not having any luck so far and the lack of logs isn't helping point me in any direction.
I'm using Android Studio and have gradle setup for the libs in dependencies:
compile files('libs/core-3.1.1.jar')
compile files('libs/android-integration-3.1.1.jar')
Help is really appreciated.
The problem is not with zxing, but rather with the way you start it. The second parameter of the startActivityForResult method is a requestCode (documentation). You would then check that requestCode on onActivityResult to handle the response. Also, do not call super first in onActivityResult.
For example:
startActivityForResult(intent, 123);
will cause Android to call onActivityResult with request code 123
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 123 && resultCode == RESULT_OK) {
//do some stuff...
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Try this
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
...
}
try to implement this, hope it might work.
in onCrearte() -- call the doScan() method.
//OnClick of Scan button.
Button btn_scan = (Button) findViewById(R.id.stbcheck_btn_scan);
btn_scan.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
doScan();
}
});
doScan():
//Bar code Scanning Option and API Integration
public void doScan() {
IntentIntegrator.initiateScan(this);
}
onActivityResult():
public void onActivityResult(int request, int result, Intent i) {
IntentResult scan=IntentIntegrator.parseActivityResult(request, result, i);
if (scan!=null) {
//format.setText(scan.getFormatName());
//Set the returned content from Scanner to edit text
et_stbNo.setText(scan.getContents());
}
}
NOTE: You must call these onSavedInstanceState() and onRestoreInstanceState() methods, as your activity is destroyed and recreated again while changing the orientation.
#Override
public void onSaveInstanceState(Bundle state) {
//state.putString("format", format.getText().toString());
state.putString("contents", et_stbNo.getText().toString());
}
#Override
public void onRestoreInstanceState(Bundle state) {
//format.setText(state.getString("format"));
et_stbNo.setText(state.getString("contents"));
}
Hope it helps:
Use below code it is working for my code.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null)
{
if (result.getContents() == null)
{
}
else
{
}
}
else
{
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
Related
I have to throw in the towel on this. I am trying to use Image Cropper: Arthur Hub in a Fragment and I keep getting this
error: onActivityResult(int,int,Intent) in ProfileFragment cannot
override onActivityResult(int,int,Intent) in Fragment attempting to
assign weaker access privileges; was public
Here is the imageCropper function in the fragment:
private void ImagePicker() {
CropImage.activity(mainImageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(startActivityForResult();,this);
}
And here is the onActivityResult in the same fragment I am using to obtain the image:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == Activity.RESULT_OK) {
mainImageUri = result.getUri();
profileImage.setImageURI(mainImageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
I had this implemented previously in an activity and it worked fine. As soon as I adjusted it to work in a Fragment, I cannot proceed.
Please help! Also I am a relatively new developer so please be a bit more descriptive in your explanation. Thanks!
In fragment overrides onActivityResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { //note: public, not protected.
super.onActivityResult(requestCode, resultCode, data);
}
and in fragment call startActivityForResult method.
I am creating an app that has the LinkedIn login. I am following this documentation. But when I click on the login button, the app redirected to the LinkedIn app and asking for login. When successful login it redirected to my app. But nothing happens. Nothing happens on onActivityResult also.
Below is my code .Login implemented on fragment
LISessionManager.getInstance(getActivity()).init(getActivity(), buildScope(), new AuthListener() {
#Override
public void onAuthSuccess() {
getLinkedInProfile();
Toast.makeText(getApplicationContext(), "success" , Toast.LENGTH_LONG).show();
}
#Override
public void onAuthError(LIAuthError error) {
Toast.makeText(getApplicationContext(), "failed " + error.toString(), Toast.LENGTH_LONG).show();
}
}, true);
//
private static Scope buildScope() {
return Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS);
}
and onActivityResult as follows:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LISessionManager.getInstance(getActivity()).onActivityResult(getActivity(), requestCode, resultCode, data);
}
Already added hash and package name on LinkedIn developer console. Did I am missing anything? Please help
It is found that the onActivityResult for LinkedIn sdk triggres on the parent activity rather than fragment onActivityResult. So you have to write following code into your parent Activity's onActivityResult for triggering fragment's onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
yourFragment.onActivityResult(requestCode, resultCode, data);
}
Try Log to ensure whether function is called.
I have two application A and B.
Application A
the following code I launch B Application for get Result from A application activity ..
String packageName = "com.cm.applicationb";
PackageManager manager = context.getPackageManager();
Intent i = manager.getLaunchIntentForPackage(packageName);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.putExtra("grand_total", "2500");
i.setAction(packageName);
startActivityForResult(i, REQUEST_DATA);
From Application A call onActivityResutl
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_DATA)
if (resultCode == Activity.RESULT_OK) {
me=data.getStringExtra("Obj");
}
Toast.makeText(getApplicationContext(),me,Toast.LENGTH_LONG).show();
}
B Application setResult Data
Intent i=new Intent();
i.putExtra("Obj", "object");
setResult(Activity.RESULT_OK, i);
finish();
Ever retrun null data and resultcode from onActivityResult. How we can solve it? It is impossible get data onAcitivityResult from another applicaion setREsult. Anyone please help me for us. Thank you so much.
Remove finish() and call like this in B:
#Override
public void onBackPressed() {
Intent data = new Intent();
Bundle bundle = new Bundle();
bundle.putString("Obj", "Obj_Data");
data.putExtras(bundle);
setResult(Activity.RESULT_OK, data);
super.onBackPressed(); // this calls finish(); internally.
}
just add this line to your manifest.xml
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
for more detail, check this links that explain about how to receive data from other application
So whenever I select a picture from the gallery in my app, it crashes. Here is the code for the button to the gallery and selected picture to the imageview.
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() { //opens the gallery
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView2.setImageURI(imageUri);
}
}
Please comment if you need more information, I desperately need help, as this is a very major roadblock for me.
Without the Logs, I can recommend on the following:
Make sure that pickImageButton isn't null.
Make sure that imageView2 isn't null.
Make sure that in manifest your Activity is in:
android:launchMode="singleTop"
I need to unserstand how I can record video programatically. Now I use this construction:
public class AndroidLearningActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(captureVideoIntent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
When the application is opened then the camera will be opened too. I have record some video, but if I press "back" button on the device then the application crushes. Please, explain me, how can I do it? Thank you.
looks as you are pressing back key and data (intent) not get set.so data may be null here
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
You have problem in this statement
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
When you will press back button recording will be cancelled and you will get data.getData as null since no recording is done.So change your code to following.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
Log.e("result", "result:" + resultCode);
}
super.onActivityResult(requestCode, resultCode, data);
}