我有2个活动A,B,两个活动都使用onActivityResult()
。
过程:
onActivityResult()
罚款。onActivityResult()
(在下面的代码中显示)。第一个作为可点击的TextView
,它也正常工作。另一个是Button
。我面临的问题是在Button中,它假设从B的子活动中带来一个Bitmap并在B中的ImageView中显示。当我点击按钮时,它会转到活动A的onActivityResult()
。
startActivityForResult()
:
int capSig = arg0.getId();
if(capSig == R.id.capSig) //Button which takes me to sub-activity of B
{
Intent goToCapSignatures = new Intent(this, CaptureSignature.class);
startActivityForResult(goToCapSignatures, GET_SIG);
}
我如何将ByteArray更改为位图并将位图发送到活动B:
Bitmap returnedBitmap = Bitmap.createBitmap(mContent.getWidth(),
mContent.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = mContent.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
mContent.draw(canvas);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
returnedBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
byte[] byteArray = bs.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length);
//send the captured signature to Check and Operations page
Intent returncapSigIntent = new Intent();
returncapSigIntent.putExtra("signature",bitmap);
setResult(RESULT_OK, returncapSigIntent);
finish();
活动B中的 onActivityResult()
:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == GET_NOTES) // For textview (working fine)
{
if(resultCode == RESULT_OK)
{
if (data.hasExtra("notes ready"))
{
String readyNotes = data.getExtras().getString("notes ready");
showNotesFromNotesClass.setText(readyNotes);
}
}
if (requestCode == GET_SIG) // for Button - this isn't being
called instead
{
if(resultCode == RESULT_OK)
{
if (data.hasExtra("signature"))
//display Bitmap in an ImageView
capturedSigImageFromCapSigclass = (Bitmap) data.getExtras().get("signature");
imgSig.setImageBitmap(capturedSigImageFromCapSigclass);
}
}
}
}
}
活动A中的 onActivityResult()
:(这被称之为。
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK && requestCode == REQUEST_APPLIANCE) {
if (data.hasExtra("selected appliance"))
{
String selectedAppType = data.getExtras().getString("selected appliance");
tvApplianceType.setText(selectedAppType);
Toast.makeText(this, data.getExtras().getString("selected appliance"),
Toast.LENGTH_SHORT).show();
}
}
}
我确定我正在做一些工作。如果我出错了,请给我一些指示或指导。我将非常感激,因为我已经在这个逻辑上度过了几天而没有到达任何地方。感谢
答案 0 :(得分:0)
您只需更改每个活动的requestCode值,以确保只调用一个。
requestsCode只是int常量,它们只是代表!
private static final int GET_NOTES = 0;
...
private static final int GET_SIG = 1;
...
private static final int REQUEST_APPLIANCE = 2;
这应该足以使结果多样化