在我的应用程序中,我使用Tab。有三个选项卡。其中第三个选项卡是具有两个活动的活动组。在第一个活动中,有两个用户选项。用户可以从相机或图库中选择图像。选择图像后,用户应移动到子活动,该子活动将在该活动中显示所选图像。下面是我的代码...
public class GalleryPic extends Activity {
private static final int TAKE_PICTURE = 0;
private static final int REQUEST_ID = 1;
private static final int HALF = 2;
private Uri mUri;
private Bitmap mPhoto;
ImageView ivGallery, ivCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_pic);
ivGallery = (ImageView)findViewById(R.id.gallery);
ivCamera = (ImageView)findViewById(R.id.snap);
ivGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
Intent in = new Intent(getParent(), GalleryPic_Second.class);
TabGroupActivity parent = (TabGroupActivity) getParent();
parent.startChildActivity("ArrowsActivity", in);
startActivityForResult(intent, REQUEST_ID);
}
});
ivCamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
mUri = Uri.fromFile(f);
Intent in = new Intent(getParent(), GalleryPic_Second.class);
TabGroupActivity parent = (TabGroupActivity) getParent();
parent.startChildActivity("ArrowsActivity", in);
startActivityForResult(i, TAKE_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK)
{
getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getContentResolver();
Intent previewMessage = new Intent(getParent(), GalleryPic_Second.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("ArrowsActivity", previewMessage);
try
{
mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
Intent in = new Intent(getParent(), GalleryPic_Second.class);
GalleryPic_Second.bmp = mPhoto;
TabGroupActivity parent = (TabGroupActivity) getParent();
parentActivity.startChildActivity("ArrowsActivity", in);
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
break;
case REQUEST_ID :
InputStream stream = null;
if (resultCode == Activity.RESULT_OK) {
try
{
stream = getContentResolver().openInputStream(data.getData());
Bitmap original = BitmapFactory.decodeStream(stream);
Intent in = new Intent(getParent(), GalleryPic_Second.class);
GalleryPic_Second.bmp = Bitmap.createScaledBitmap(original, original.getWidth()/HALF, original.getHeight()/HALF, true);
TabGroupActivity parent = (TabGroupActivity) getParent();
parent.startChildActivity("ArrowsActivity", in);
}
catch (Exception e)
{
e.printStackTrace();
}
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
@Override
public void onBackPressed() {
// this.getParent().onBackPressed();
TabViewLayout parentTab = (TabViewLayout) this.getParent();
parentTab.switchTabBar(1);
}
}
我在这里面临的是,我没有在子活动组中获得选定的图像。我已经搜索了它并得出了这个解决方案......OnActivityResult 在 TabActivityGroup 中不起作用?但我没有得到此链接中给出的解释。需要做什么?
把你的 ActivityResult 代码放在 GroupActivity 意味着在父项中,因为它将值返回给它的父级而不是子级。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
}
}
上面的代码放在 活动组 .我希望它能为你工作,因为它对我有用,祝你好运:-)。