1

I'm currently working on a app which the user can answer a question through images by taking photos or uploading photos from the album.

I've separated into two classes which are FulfillPhotoTaskActivity, AddPhotoActivity. FulfillPhotoTaskActivity have imageView, addphoto button and save button. when I press addphoto button, it goes to AddPhotoActivity which we can select two options(taking photos or uploading photos). I finished the part where if the user press take photo button then it opens the camera and take the photo. I created onActivityResult which get the image data. but in many example, inside the onActivityResult they also have imageView, like setImageBitmap.

My problem is, how can I get the imageView from the AddPhotoActivity and shows it in FulfillPhotoTaskActivity which I already made for XML.

Derrick Park
  • 67
  • 1
  • 4

1 Answers1

0

When you take a photo it saves the image to a file. You can retrieve this image file and then you can call your imageView in FulfillPhotoTaskActivity and set the imageView to the image that is in the file.

The following code will start the camera intent and the file to a given location:

    File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder/SubFolder");

    String folder = root.toString(); 
    File file = new File(folder, "fileName" + ".jpg");
    Uri outputFileUri = Uri.fromFile(file);

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(cameraIntent, 0);

Now when the user takes a picture you know where it will be saved. In your FulfillPhotoTaskActivity you can call your imageview:

    ImageView imageView = (ImageView)findViewById(R.id.YOUR_IMAGE_VIEW_ID);

Finally, you can just set the image to the imageview:

    imageView.setImageBitmap(BitmapFactory.decodeFile(file));

So, you don't need to get the imageview from AddPhotoActivity. You only need the file name. Then, you can go back to FulfillPhotoTaskActivity and set the imageview to the bitmap file.

I hope this helps!

chRyNaN
  • 3,592
  • 5
  • 46
  • 74
  • Thank you so much! but here imageView.setImageBitmap(BitmapFactory.decodeFile(file)); should I put .decodeFile(AddPhotoActivity.file)); ??cause I'm getting the file from the other class(AddPhotoActivity) – Derrick Park Nov 04 '12 at 03:06
  • One way is when you call the activity, you could use startActivityForResult(). Then, you could use the setResult() method to put the extra value in. Here's a [link](http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android) referring to setResult. Or you could use sharedPreferences. Here's a [link](http://mobile.tutsplus.com/tutorials/android/android-application-preferences/) referring to sharedPreferences. Hopefully, this works – chRyNaN Nov 04 '12 at 04:14