我正在开发一个涉及QR扫描的应用程序。我能够执行从我的应用程序调用的扫描程序应用程序库。扫描后我需要存储捕获的值,但是我的应用程序中写入的onActivityResult方法没有被调用。请帮忙 我在这里发布代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
// Intent intent = new
// Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == 1) {
// Handle successful scan
String capturedQrValue = intent.getStringExtra("RESULT");
// String format =
intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(MainActivity.this,"Scan Result:" + capturedQrValue, Toast.LENGTH_SHORT).show();
finish();
Intent it = new Intent(MainActivity.this, ThirdActivity.class);
it.putExtra("Code", capturedQrValue);
startActivity(it);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
答案 0 :(得分:0)
更改你的onActivityResult这样的事情,这可能会有所帮助
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == 1) {
// Handle successful scan
String capturedQrValue = intent.getStringExtra("RESULT");
// String format =
intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(MainActivity.this,"Scan Result:" + capturedQrValue, Toast.LENGTH_SHORT).show();
finish();
Intent it = new Intent(MainActivity.this, ThirdActivity.class);
it.putExtra("Code", capturedQrValue);
startActivity(it);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
答案 1 :(得分:0)
RESULT_OK是int类型。
它的值等于-1
android.app.Activity.RESULT_OK = -1 [0xffffffff] 参考网址 http://developer.android.com/reference/android/app/Activity.html#RESULT_OK
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// Handle successful scan
String capturedQrValue = intent.getStringExtra("RESULT");
// String format =
intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(MainActivity.this,"Scan Result:" + capturedQrValue, Toast.LENGTH_SHORT).show();
finish();
Intent it = new Intent(MainActivity.this, ThirdActivity.class);
it.putExtra("Code", capturedQrValue);
startActivity(it);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
答案 2 :(得分:0)
使用以下代码进行意图形式主要活动以捕获活动,但仅限于您使用zxing库作为第三方
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
像这样更新你的onActivityResult方法。
//method for getting qr code from qr code image
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
Toast.makeText(MainActivity.this,"Scan Result:" + contents , Toast.LENGTH_SHORT).show();
Intent it = new Intent(MainActivity.this, ThirdActivity.class);
it.putExtra("Code", contents );
startActivity(it);
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.d("the result is","not okjn");
}
}
}
答案 3 :(得分:0)
这是ZXing QR Code Scanner Library使用这个我们可以扫描任何QR码..
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.dm.zbar.android.scanner.ZBarConstants;
import com.dm.zbar.android.scanner.ZBarScannerActivity;
public class MainActivity extends Activity {
private static final int ZBAR_SCANNER_REQUEST = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (isCameraAvailable()) {
try {
Intent intent = new Intent(MainActivity.this, ZBarScannerActivity.class);
startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, "Activity " + e.getLocalizedMessage().toString(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getLocalizedMessage().toString(), Toast.LENGTH_SHORT)
.show();
}
} else {
Toast.makeText(MainActivity.this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ZBAR_SCANNER_REQUEST:
if (resultCode == RESULT_OK) {
if (data.getStringExtra(ZBarConstants.SCAN_RESULT).length() > 0) {
Toast.makeText(this, "" + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(this, "No Event ID Found", Toast.LENGTH_SHORT).show();
}
} else if (resultCode == RESULT_CANCELED && data != null) {
String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
if (!TextUtils.isEmpty(error)) {
Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
}
}
break;
}
}
public boolean isCameraAvailable() {
PackageManager pm = getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
请在manifest.xml中提及这些权限
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.qrscandemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.dm.zbar.android.scanner.ZBarScannerActivity"
android:screenOrientation="portrait" />
</application>
这是使用的图书馆.... Library Project