安卓zxing intentintegrator [英] android zxing intentintegrator
问题描述
我已经写了下面的code,如果你决定要扫描QR code(使用zxing)并将其存储在专用存储,但如果你决定要取消扫描工作正常,崩溃和文件previously存储的内容就会消失。
我想这可能是一个设计错误,不知道为什么。
下面是有关code
.../ **
*菜单生成
* /
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
MenuInflater吹气= getMenuInflater();
inflater.inflate(R.menu.menu,菜单);
返回true;
}/ **
*菜单处理
* /
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
上下文的背景下= getApplicationContext();
烤面包烤面包= Toast.makeText(上下文,,Toast.LENGTH_LONG);
toast.setGravity(Gravity.FILL_HORIZONTAL,0,0);
开关(item.getItemId()){
案例R.id.qrScan:
IntentIntegrator积分=新IntentIntegrator(本);
integrator.initiateScan();
返回true;
案例R.id.qrReset:
文件DIR = getFilesDir();
档案文件=新的文件(目录,QR codeFILE);
布尔删除= file.delete();
返回true;
案例R.id.appClose:
this.finish();
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}... 公共无效的onActivityResult(INT申请code,INT结果code,意图意图){
IntentResult scanResult = IntentIntegrator.parseActivityResult(要求code,结果code,意向);
上下文的背景下= getApplicationContext();
烤面包烤面包= Toast.makeText(上下文,,Toast.LENGTH_SHORT);
如果(scanResult!= NULL){
FOS的FileOutputStream = NULL;
CharSequence的文字= scanResult.getContents();
尝试{
FOS = openFileOutput(QR codeFILE,Context.MODE_PRIVATE);
尝试{
fos.write(text.toString()的getBytes());
fos.close();
toast.setGravity(Gravity.FILL_HORIZONTAL,0,0);
toast.setText(code保存);
toast.show();
}赶上(IOException异常前){
toast.setGravity(Gravity.FILL_HORIZONTAL,0,0);
toast.setText(无效code);
toast.show();
。Logger.getLogger(MainActivity.class.getName())日志(Level.SEVERE,空,前);
}
}赶上(FileNotFoundException异常前){
toast.setGravity(Gravity.FILL_HORIZONTAL,0,0);
toast.setText(错误,同时节省);
toast.show();
。Logger.getLogger(MainActivity.class.getName())日志(Level.SEVERE,空,前);
}
}其他{
toast.setGravity(Gravity.FILL_HORIZONTAL,0,0);
toast.setText(无效code);
toast.show();
}
}
您需要检查结果code
。如果扫描被取消,没有了吧code值的信息。随着结果code
您可以检查是否操作成功与否。
如果(结果code == RESULT_OK){
//扫描成功
}其他{
// 没有结果
}
I've written the following code that works fine if you decide to scan a QR code (using zxing) and store it in private storage but in case you decide to cancel scanning, it crashes and the file previously stored content disappears.
I think it might be a design error, not sure why.
Below is relevant code
...
/**
* menu generation
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
/**
* menu handling
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
switch (item.getItemId()) {
case R.id.qrScan:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
return true;
case R.id.qrReset:
File dir = getFilesDir();
File file = new File(dir, qrCodeFile);
boolean deleted = file.delete();
return true;
case R.id.appClose:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
...
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
if (scanResult != null) {
FileOutputStream fos = null;
CharSequence text = scanResult.getContents();
try {
fos = openFileOutput(qrCodeFile, Context.MODE_PRIVATE);
try {
fos.write(text.toString().getBytes());
fos.close();
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.setText("Code saved");
toast.show();
} catch (IOException ex) {
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.setText("Invalid code");
toast.show();
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.setText("Error while saving");
toast.show();
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.setText("Invalid code");
toast.show();
}
}
You need to check the resultCode
. If the scan is canceled there is no information about the barcode value. With the resultCode
you can check if the operation was successful or not.
if(resultCode == RESULT_OK) {
// scan successful
} else {
// no result
}
这篇关于安卓zxing intentintegrator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!