我尝试在ResultActivity类上打开结果,但这不是为什么?
**我尝试从图库中的图像中扫描二维码**
任何解决方案
这是代码
case R.id.scn_img:
Intent pickIntent = new Intent(Intent.ACTION_PICK);
pickIntent.setDataAndType( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, 111);
break;
这是onActivityResult代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//the case is because you might be handling multiple request codes here
case 111:
if(data == null || data.getData()==null) {
Log.e("TAG", "The uri is null, probably the user cancelled the image selection process using the back button.");
return;
}
Uri uri = data.getData();
try
{
InputStream inputStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (bitmap == null)
{
Log.e("TAG", "uri is not a bitmap," + uri.toString());
return;
}
int width = bitmap.getWidth(), height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
bitmap.recycle();
bitmap = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
try
{
Result result = reader.decode(bBitmap);
//here i want to do the code but this not working
// here is the problem
//********************************
ResultActivity.startResultActivity(BaseActivity.this, result);
//********************************
// toast message it is work good
Toast.makeText(this, "The content of the QR image is: "
+ result.getText(), Toast.LENGTH_SHORT).show();
}
catch (NotFoundException e)
{
Log.e("TAG", "decode exception", e);
}
}
catch (FileNotFoundException e)
{
Log.e("TAG", "can not open file" + uri.toString(), e);
}
break;
}
}
这是我的ResultActivity类代码
public class ResultActivity extends AppCompatActivity {
private static final String HISTORY_DATA = "ResultActivity.HISTORY_DATA";
private static final int REQUEST_WRITE_PERMISSION = 200;
private static BarcodeResult barcodeResult = null;
private static HistoryItem historyItem = null;
private ResultViewModel viewModel;
private ResultFragment currentResultFragment;
public static void startResultActivity(@NonNull Context context, @NonNull BarcodeResult barcodeResult) {
ResultActivity.barcodeResult = barcodeResult;
ResultActivity.historyItem = null;
Intent resultIntent = new Intent(context, ResultActivity.class);
context.startActivity(resultIntent);
}
public static void startResultActivity(@NonNull Context context, @NonNull HistoryItem historyItem) {
ResultActivity.barcodeResult = null;
ResultActivity.historyItem = historyItem;
Intent resultIntent = new Intent(context, ResultActivity.class);
resultIntent.putExtra(HISTORY_DATA, true);
context.startActivity(resultIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
viewModel = ViewModelProviders.of(this).get(ResultViewModel.class);
initStateIfNecessary(savedInstanceState);
ActionBar ab = getSupportActionBar();
if(ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
if(isFinishing()) {
return;
}
loadFragment(viewModel.mParsedResult);
displayGeneralData();
}
private void initStateIfNecessary(Bundle savedInstanceState) {
boolean hasHistoryItem = getIntent().getBooleanExtra(HISTORY_DATA, false);
if(savedInstanceState == null) {
if(hasHistoryItem && historyItem != null) {
viewModel.initFromHistoryItem(historyItem);
} else if(barcodeResult != null) {
viewModel.initFromScan(barcodeResult);
} else {
// no data to display -> exit
Toast.makeText(this, R.string.activity_result_toast_error_cant_load, Toast.LENGTH_SHORT).show();
finish();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share,menu);
getMenuInflater().inflate(R.menu.copy,menu);
getMenuInflater().inflate(R.menu.save, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(menu != null) {
MenuItem saveMi = menu.findItem(R.id.save);
if(saveMi != null) {
saveMi.setVisible(!viewModel.mSavedToHistory);
}
}
return true;
}
public void onClick(View view) {
if (view.getId() == R.id.btnProceed) {
if(currentResultFragment != null) {
currentResultFragment.onProceedPressed(this);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent sharingIntent= new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, viewModel.mParsedResult.getDisplayResult());
startActivity(Intent.createChooser(sharingIntent,getString(R.string.share_via)));
return true;
case R.id.save:
viewModel.saveHistoryItem(viewModel.currentHistoryItem);
invalidateOptionsMenu();
Toast.makeText(this, R.string.activity_result_toast_saved, Toast.LENGTH_SHORT).show();
return true;
case R.id.copy:
ClipboardManager clipboardManager =(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("Text", viewModel.mParsedResult.getDisplayResult());
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(getApplicationContext(), R.string.content_copied, Toast.LENGTH_SHORT).show();
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void loadFragment(@NonNull ParsedResult parsedResult) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ResultFragment resultFragment;
switch (parsedResult.getType()) {
case ADDRESSBOOK:
resultFragment = new ContactResultFragment();
break;
case EMAIL_ADDRESS:
resultFragment = new EmailResultFragment();
break;
case PRODUCT:
resultFragment = new ProductResultFragment();
break;
case URI:
resultFragment = new URLResultFragment();
break;
case GEO:
resultFragment = new GeoResultFragment();
break;
case TEL:
resultFragment = new TelResultFragment();
break;
case SMS:
resultFragment = new SMSResultFragment();
break;
case WIFI:
resultFragment = new WifiResultFragment();
break;
case TEXT:
default:
resultFragment = new TextResultFragment();
break;
}
currentResultFragment = resultFragment;
resultFragment.putQRCode(parsedResult);
ft.replace(R.id.activity_result_frame_layout, resultFragment);
ft.commit();
}
}
错误消息是
无法解析方法startResultActivity com.google.zxing.Result
我想举一个例子
谢谢
答案 0 :(得分:0)
在startResultActivity
中没有名称为ResultActivity
的静态方法,该方法使用Context
和Result
作为参数
BarcodeResult与结果不同
您需要创建带有签名public static void startResultActivity(@NonNull Context context, @NonNull Result result) {