gpt4 book ai didi

android - StartActivityForResult 未将数据返回给父 Activity

转载 作者:行者123 更新时间:2023-11-29 01:13:39 24 4
gpt4 key购买 nike

有三个 Activity A、B、C,在 Activity A 中一个 Intent 打开 Activity B。在 Activity B 上调用 startActivityForResult 并打开 C。但是在 C 中调用 setResult 之后,它返回到 Activity A 而不是 B我正在复制我的 Activity 代码,我正在调用开始 Activity 创建事件

     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_FILE) {
if (resultCode == Activity.RESULT_OK) {

alertDialogBox.dismiss();
Intent image = new Intent(CreateEventActivity.this, CropImageActivity.class);
image.putExtra("data", data.getData().toString());
startActivityForResult(image, 2);

}

}
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
String bt = data.getStringExtra("result");
file_path = data.getStringExtra("filepath");
testPath = new File(file_path);
Log.e("desti", testPath.toString());
Log.e("destpat", testPath.getAbsolutePath());
try {
testPath.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

try {
byte[] encodeByte = Base64.decode(bt, Base64.DEFAULT);
bitmap2 = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);

// imageforUpload = getStringImage(bitmap2);
BitmapFactory.Options options = new BitmapFactory.Options();

// down sizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;

final Bitmap bitmaptest = BitmapFactory.decodeFile(file_path, options);

imgCropped.setVisibility(View.VISIBLE);
imgCropped.setImageBitmap(bitmaptest);


} catch (Exception e) {
e.getMessage();

}
}
}
}

第二个 Activity cropActivity,我将数据返回给父级

     private final CropCallback mCropCallback = new CropCallback() {
@Override
public void onSuccess(Bitmap cropped) {
int height = cropped.getHeight();
int width = cropped.getWidth();
if (height<400||width<400){
Toast.makeText(getApplicationContext(),"this image cat be cropped",Toast.LENGTH_SHORT).show();
}else {

Bitmap bitmap = Bitmap.createBitmap(cropped, 0, 0, 400, 400);


imgtest.setImageBitmap(bitmap);
SaveImage(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);


Intent returnIntent = new Intent();
returnIntent.putExtra("result", temp);
returnIntent.putExtra("filepath", filePath);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}

请帮我解决这个问题,返回页面是另一个 Activity

最佳答案

希望你必须更正你的requestCode

Intent returnIntent = new Intent();
returnIntent.putExtra("result", temp);
returnIntent.putExtra("filepath", filePath);
setResult(2, returnIntent);
finish();

因为你在这里提到了同样的

Intent image = new Intent(CreateEventActivity.this, CropImageActivity.class);
image.putExtra("data", data.getData().toString());
startActivityForResult(image, 2);

关于android - StartActivityForResult 未将数据返回给父 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41295747/

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