I trying to return the captured image and text from Activity B to Activity A listView. I follow this tutorial 1 and tutorial 2, but sadly, the list number was fixed. I get stucked on some part since I wanted to add the listView dynamically but not fixed. Can someone help me ? Thanks
Activity B (return text and image to A)
private void activeTakePhoto() { // open camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE &&
resultCode == RESULT_OK && null != data) {
......// selecte from gallery
}
case REQUEST_IMAGE_CAPTURE:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
//to generate random file name
String fileName = "tempimg.jpg";
try {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo); // image can shown here
} catch (Exception e) {
e.printStackTrace();
}
}
}
submit.setOnClickListener(new View.OnClickListener() { // return image and text to A
@Override
public void onClick(View v) {
Intent returnIntent=new Intent();
amount=Amount.getText().toString();
returnIntent.putExtra("amount", amount);
returnIntent.putExtra("photo",photo);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
Activity A
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (resultCode == RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
ReceiveAmount = data.getStringExtra("amount");
ReceiveImage=data.getStringExtra("photo");
if (mClickedPosition == -1) { // if icon clicked
if (obj != null)
obj.addNewItem( ReceiveAmount,ReceiveImage);
} else {
if (obj != null)
obj.changeItem(mClickedPosition,ReceiveAmount, ReceiveImage);
}
}
}
}
PicCustomBaseAdapter(obj)
public void addNewItem(String amount, Bitmap imageFromClaims)
{
ImageAndText image = new ImageAndText();
image.setAmount(" Amount : " + amount);
image.setImage(imageFromClaims);
imgAndText.add(image);
this. notifyDataSetChanged();
addOrRemoveFooter();
}
public void changeItem(int m,String amount, Bitmap imageFromClaims)
{
ImageAndText image = new ImageAndText();
image.setAmount(" Amount : " + amount);
image.setImage(imageFromClaims);
imgAndText.set(image);
this. notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_image_and_text, null);
holder = new ViewHolder();
holder.txtAmount = (TextView) convertView.findViewById(R.id.ListAmount);
holder.picture=(ImageView)convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtAmount.setText(imgAndText.get(position).getAmount());
holder.picture.setImageResource(imgid[imgAndText.get(position).getImageNumber() - 1]);
return convertView;
}
static class ViewHolder {
TextView txtAmount;
ImageView picture;
}
}
Finally ImageAndText
private String amount = "";
Bitmap image;
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getAmount() {
return amount;
}
My problem :
1.ReceiveImage in onActivityResult Activity A wrong 2nd Argument Type
2. In PicCustomBaseAdapter, public view get view , how should I put imgid `holder.picture.setImageResource(imgid[imgAndText.get(position).getImageNumber() - 1]);` since image is added dynamically ?
3. imgAndText.set(image); in PicCustomBaseAdapter, changeItem() error
getView()method, you use agetImageNumber()method not shown in theImageAndTextclass. I'm assuming that's a Resource ID. You need to be consistent with how you're handling the image in that class. I would change theImageAndTextto remove any use of a Resource ID; i.e. have it keep only a Bitmap. When you're creating the initial data list, load the Bitmap from Resources, and pass that to theImageAndTextobject, instead of an ID. Then change thegetView()line to:holder.picture.setImageBitmap(imgAndText.get(position).getImage());.