英文:
How to fix this problem cannot resolve method startResultActivity com.google.zxing.Result?
问题
我尝试在ResultActivity类中打开结果,但为什么无法呢?
我尝试从图库中的图像扫描QR码
有什么解决方案吗?
以下是代码示例:
// 这是扫描图像的代码
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) {
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;
}
// 其他图像处理逻辑...
} catch (FileNotFoundException e) {
Log.e("TAG", "can not open file" + uri.toString(), e);
}
break;
}
}
// 这是ResultActivity类的代码
public class ResultActivity extends AppCompatActivity {
// ... 其他代码 ...
// 这是用来启动ResultActivity的静态方法
public static void startResultActivity(@NonNull Context context, @NonNull BarcodeResult barcodeResult) {
// 启动ResultActivity的逻辑...
}
// ... 其他代码 ...
// 这是启动ResultActivity的示例
ResultActivity.startResultActivity(BaseActivity.this, result);
// ... 其他代码 ...
}
希望这个示例对你有所帮助。如有问题,请随时问我。
英文:
i try to open the result on ResultActivity class but it cant be why?
**i try to scan qr code from image from the gallery **
any solution
here is the code
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;
this is onActivityResult code
@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;
}
}
here is my ResultActivity class code
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();
}
}
error message it is
cannot resolve method startResultActivity com.google.zxing.Result
I want to answer with an example
Thank you
答案1
得分: 0
在ResultActivity
中没有名为startResultActivity
的静态方法,该方法接受Context
和Result
作为参数
BarcodeResult
与Result
不相同
您需要创建一个方法,其签名为public static void startResultActivity(@NonNull Context context, @NonNull Result result) {
。
英文:
There is no static method by the name of startResultActivity
in ResultActivity
that takes Context
and Result
as arguments
BarcodeResult is not the same as Result
You need to create a method with the signature public static void startResultActivity(@NonNull Context context, @NonNull Result result) {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论