gpt4 book ai didi

java - 在现有项目上集成 git 项目 startActivityForResult 和 onActivityResult android

转载 作者:行者123 更新时间:2023-12-01 22:30:05 25 4
gpt4 key购买 nike

我实现了这个git在我当前的项目中,没有克隆

implementation 'com.github.adityaarora1:LiveEdgeDetection:master-SNAPSHOT'

但我无法在我的方法上调用它。文档说

  1. 从您的 Activity 中启动 startActivityForResult

    startActivityForResult(new Intent(this, ScanActivity.class), REQUEST_CODE);
  2. 在 onActivityResult 上获取裁剪图像的文件路径

    String filePath =  data.getExtras().getString(ScanConstants.SCANNED_RESULT);
    Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);

所以我尝试从新类中像这样调用 onClick 按钮

   scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivity(myIntent);
}

并将其余部分放入我的 onActivityResult

 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
}

编辑:这是作者在 git 上使用的 MainActivity 我尝试使用它,但收到此错误:

scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);

MainActivity(已导入)

private static final int REQUEST_CODE = 101;
private ImageView scannedImageView;
Button scan;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
startScan();

scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(ScanActivity2.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});
}

private void startScan() {
Intent intent = new Intent(this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}

更新:

经过一番研究,我发现导入的项目仅读取文件且无法更改(ScanActivity.java),并且我当前的项目已更新 sdk 28,这与导入的项目不同,因此 ScanActivity 中存在一些错误,即为什么按钮(技术上)不起作用

最佳答案

您应该使用 startActivityForResult 而不是如下所示的 startActivity

scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});

并修改您的onActivityResult,例如

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(resultCode == Activity.RESULT_OK && requestCode == 111){
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
Log.d("YourTAG","File Path "+filePath);
// here you can set bitmap to your image view
yourImageView.setImageBitmap(baseBitmap);
}
}

更新

您的主要 Activity 应该是这样的

private static final int REQUEST_CODE = 111;
private ImageView scannedImageView;
Button scan;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(R.id.scanned_image); // this ImageView should be in your activity_scan.xml file with same id(scanned_image)
startScan();

scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startScan();
}
});
}

private void startScan() {
Intent intent = new Intent(ScanActivity2.this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}

演示项目

我已在 Github 上上传了一个演示项目,该项目集成了 LiveEdgeDetection 库并按预期工作。检查一下go here

关于java - 在现有项目上集成 git 项目 startActivityForResult 和 onActivityResult android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58552154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com