Android camera (getting imageView from a different class)


Ask by : Derrick Park November 04, 2012 00:05

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.

View original question

Answer by : chRyNaNNovember 04, 2012 00:05

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!

View original answer