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.
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!
Related
My application is capable of capturing images and saving them (in the public Pictures directory, retrieved by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).
Now, I want to be able to display those images in the system's gallery application. In order to achieve that, I create an Intent like this (filePath is a String containing the image's path):
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filePath));
viewIntent.setDataAndType(uri, "image/jpeg");
startActivity(Intent.createChooser(viewIntent, null));
Displaying an image this way works perfectly fine, but the gallery's Share functionality doesn't seem to work:
When I press the Share-button, I get to choose the app I want to share to; but once I select it, nothing happens, as if the target application does not receive any data.
Is there any explanation why this behaviour occurs and how it can be fixed?
Edit: I used Intent Intercept to capture the Intent created by the Gallery/Photos app:
I know the basics on how to take a picture and set it to ImageView.
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
I want to do a little more than that.
I am saving it to a folder on to an SD card. That I have done successfully with this:
// intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUEST);
Here is my next question:
Not sure how to do this: What I'd like to do next: The next time I come to this Activity, I'd like to check if that image exists and assign it to that imageView.
Last days i faced this issue in one of my applications.
I'll try here to explain a little bitte what i have done.
Try to save the picture full path to a storage area or to your sharedpreferences.
Next time if you call your activity then check if a picture already exists and if you can use it.
Prepare in your xml layout an ImageView with visibility="gone" and if the point (2.) is true then you can change the visibility to visible and set the image in the view.
If the point (2.) is false then switch to camera view (SurfaceView) in order to take a new picture
You would have to use some sort of persistent storage to reference the file to check if it exists. I would just store it as a string in a preference and read it, then check if it exists and so on. Easy enough to do from the onCreate().
I know the basics on how to take a picture and set it to ImageView.
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
I want to do a little more than that.
I am saving it to a folder on to an SD card. That I have done successfully with this:
// intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUEST);
Here is my next question:
Not sure how to do this: What I'd like to do next: The next time I come to this Activity, I'd like to check if that image exists and assign it to that imageView.
Last days i faced this issue in one of my applications.
I'll try here to explain a little bitte what i have done.
Try to save the picture full path to a storage area or to your sharedpreferences.
Next time if you call your activity then check if a picture already exists and if you can use it.
Prepare in your xml layout an ImageView with visibility="gone" and if the point (2.) is true then you can change the visibility to visible and set the image in the view.
If the point (2.) is false then switch to camera view (SurfaceView) in order to take a new picture
You would have to use some sort of persistent storage to reference the file to check if it exists. I would just store it as a string in a preference and read it, then check if it exists and so on. Easy enough to do from the onCreate().
I use the following code to take a picture:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path + "/" + fileName)));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Now when I use this, it does save the picture where I specify above, but it also saves a second copy to the default image folder and adds it to the gallery.
I would simply delete the second file, but it seems it would be a tad dangerous as onActivityResult's intent parameter is always null after taking said picture, so I would have to attempt deleting the most recently saved picture.
Is there any way I can prevent this behavior or correct it by getting the URI of the dupicate picture?
Well I've determined that it is pretty much not possible. I am now using a SurfaceView with my own camera activity.
I want to launch the camera from my application and want to use the captured image as an attachment in my application.
The use case is as below:
Press the capture button in the application->Opens camera application -> capture an image->the image is shown as thumbnail in my application.
Any suggestions, ideas, would be very helpful.
Image capture step description:
Get Path of image from ACTION_IMAGE_CAPTURE Intent
After that, you have path of the fetched image. Then you can use it for your purpose. Show it as small Bitmap, and attach the original to further processing.
To view captured image, send the intent
Intent viewImageIntent = new Intent();
viewImageIntent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(imageFilePath);
viewImageIntent.setDataAndType(Uri.fromFile(file), "image/*");
viewImageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(viewImageIntent);