我有一个类ExpandableListAdapter
,可以触发打开相机的意图
ImageView itemImage = (ImageView) convertView.findViewById(R.id.itemImage);
itemImage.setImageBitmap(childItem.Image);
itemImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Camera camera = new Camera(context);
// The method below fires the intent to the camera app
camera.dispatchTakePictureIntent(childItem);
}
});
childItem
是此类中的对象:
public class Item {
public String Name;
public Bitmap Image;
public boolean IsChecked;
}
相机拍摄照片并将其存储在存储卡上。
我在使用适配器的活动中覆盖了void onActivityResult(int requestCode, int resultCode, Intent data)
。
如何在活动方法中获取项目对象(我在意图中发送):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {}
编辑:这是意图被触发的地方:
public void dispatchTakePictureIntent(Item item) {
// ... I make a temp file with the name item.Name and fire the intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
((Activity) context).startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
答案 0 :(得分:0)
我假设您从Activity
调用了show camera方法。因此,只需在活动中保留一个类变量,然后在打开相机之前设置该值。
现在,您的onActivityResult
方法可以访问该值。
答案 1 :(得分:0)
您是否尝试过实现Parcelable
接口??
这是我对Parcelable接口的实现:
public class Item implements Parcelable {
int value;
String otherValue;
public Item (int value, String otherValue){
this.value = value;
this.otherValue = otherValue;
}
protected Item(Parcel in) {
value = in.readInt();
otherValue = in.readString();
}
public static final Creator<Item> CREATOR = new Creator<Item>() {
@Override
public Item createFromParcel(Parcel in) {
return new Item(in);
}
@Override
public Item[] newArray(int size) {
return new Item[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(value);
dest.writeString(otherValue);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendData(View view){
Item item = new Item(10, "otherValue");
Intent itemIntent = new Intent(MainActivity.this, ItemInfoActivity.class);
itemIntent.putExtra("data", item);
startActivity(itemIntent);
}
}
我正在使用Button组件,并且在onClick事件中调用sendData。
public class ItemInfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_info);
Item item = getIntent().getExtras().getParcelable("data");
if(item == null)
Toast.makeText(this, "NULL", Toast.LENGTH_LONG).show();
else {
Toast.makeText(this, "NOT NULL", Toast.LENGTH_LONG).show();
Log.i("CCC", "Value: " + Integer.toString(item.value));
Log.i("CCC", "OtherValue: " + item.otherValue);
}
}
}
在您的情况下,您可以使用结果活动执行相同操作,但您需要使用putExtra设置在Intent中插入Item对象,然后在 Intent data 参数中请求额外值:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
享受它!!