I'm working on a camera app which displays more than one image dynamically added to a simple LinearLayout as childs. When a specefic image is selected (onClick), it goes to Camera View (camera Intent). The App is supposed to display the new image captured in the right selected ImageView. The thing is, I don't know how to get that specific ImageView to handle it in onActivityResult method. Please help
In your onActivityResult()
if (resultCode == RESULT_OK) {
if (data != null) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(thumbnail);
}
}
Where data is the intent.
You can just save the clicked ImageView's reference before you open the Camera app, and use that reference to set the new image.
Update: Just make an ImageView variable in your activity, and before you open the Camera, put the clicked instance in this field, and use this ImageView in my example code.
Related
I am developing an app to do a lot of tasks related to images and PDF files. In one of the features I am converting images to PDF. I have encountered a few problems while handling images with bitmap.
1) When I am creating PDF file with images that I clicked using my phone's camera, for some reason they get rotated 90 degrees anti-clockwise automatically. When I am clicking the image, I used portrait mode, the image gets saved in my phone's gallery in portrait mode. When I view it in my phone's gallery it gets displayed correctly but when I load it in ImageView in Android Studio, it shows me a 90 degree anti-clockwise rotated image.
I am using the code below to load images in ImageView:
File file = new File(imagez.get(position));
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(bitmap);
Here, "imagez" is an array that contains path to all images that are selected by the user in String format. e.g., "/storage/emualted/0/pics/...." like that.
This problem is only with images that I clicked using my phone's camera, other images that I downloaded from internet or from WhatsApp or Facebook all works fine.
2) My second problem is that when I scroll the PDF that I created, the images there loads slowly. The PDF creation is completed, the images should be there all time instead they load every time as I scroll up and down as if I am using an Adapter View to inflate a List View where non-visible items gets destroyed as they move out of the view and gets loaded again when I scroll back up.
I am using itextpdf class for converting images to PDF.
EDIT :
My question is that I want the image to be displayed in the orientation it was clicked, and when I am creating the PDF, the image should be saved as original (it should not get rotated automatically).
And I also a solution to make my created PDF load the pages quickly, if it can be done.
Thank you.
The problem that you are having is a common issue. I would suggest you to try the following method:
int orientation = 0;
ExifInterface exif = new ExifInterface(path_to_your_image);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
if(orientation != 0)
{
Matrix rotateMatrix = new Matrix();
if (orientation == 6)
rotateMatrix.postRotate(90);
else if (orientation == 3)
rotateMatrix.postRotate(180);
else if (orientation == 8)
rotateMatrix.postRotate(270);
else
rotateMatrix.postRotate(0);
Bitmap newBitmap = Bitmap.createBitmap(previous_bitmap, 0, 0, previous_bitmap.getWidth(), previous_bitmap.getHeight(), rotateMatrix, false);
}
else
{
Bitmap newBitmap = Bitmap.createBitmap(previous_bitmap, 0, 0, previous_bitmap.getWidth(), previous_bitmap.getHeight);
}
//After this you can save your bitmap wherever you want
As far as your second problem is concerned I guess your images are too big. Try decreasing their size or dimensions to say 1200x1200 or whatever you feel right.
Hope it helps!
I want to take a picture in my App, so if I click on my ImageView/QuickBagde on my Fragment.
I'm able to take a picture, after that, I crop it and it return to my App. This is not a big deal and it's working well.
But how can I refresh the ImageView/QuickBadge to show up the taken picture?
I tried with:
//get the returned data
Bitmap thePic = data.getExtras().getParcelable("data");
profilePic.setImageBitmap(thePic);
But it doesn't work.
you can try this
Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
imageview.setImageBitmap(bitmap);
I know this has been asked here, but my problem is a bit different. I read other answers related to this topic on SO but they did not solve my problem.
So in my app, I have 2 activities. In the first one, there are 20 imageviews (and I am planning to add more) and in the second activity, there is a custom view which is actually a zoom image class. Basically, when the user will click on any imageview in the first acivity he will be taken to the 2nd activity where he can zoom that particular image.
What I want to do in the coding part is that get the image that is displayed on the clicked imageview and pass it to the next activity. To achieve that, I have used the below given code but it's not working. What could be wrong?
Activity_one
Intent startzoomactivity = new Intent(Activity_one.this, Activity_two.class);
Bitmap img_res = img.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", img_res);
startzoomactivity.putExtras(extras);
startActivity(startzoomactivity);
Activity_two
Intent startzoomactivity = getIntent();
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
TouchImageView img = new TouchImageView(this); // TouchImageView is the class that enables zoom feature on the imageview.
img.setImageResource(i);
img.setMaxZoom(5f);
you cannot pass the ImageView to the other activity. Views are directly tied to the activity they belong and cannot be passed around.
What you're trying to achieve in the code is to get a drawing cache copy of the ImageView as a bitmap, which even after you make it work (it's missing the view.setDrawingCache), it will give you a very bad result, specially after zooming-in.
What you need to do is find the original image (if it came from the internet the URL, or the location in your disk cache, if it's from your resources folder, the int resId that draw that image, and pass this value (string or int) to the next activity. Then you let the next activity create its own ZoomableImageView and set the original resource (link, file, resource) to it.
I understand what you want to achieve but your way is totally wrong. Instead of passing ImageView Bitmap pass the ImageView URL or Path (and if drawable then ResId) where this image coming from and pass that URL or Path or ResId to the Next Activity and load this image as a Bitmap onto ZoomableImageView.
You can get the image from imageview as a bitmap, then compress it using http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) api. then covert the outputstream object to byte array and since that is parcelable you can put this object in an intent and pass to your second activity
I have multiple imageView on my layout, want to open the camera on all Imageview simultaneously. and involve a timer of friction second. so user will move his device and photo will capture and show on ImageView one by one on click "start capture" button. Like camera will open simultaneously and first imageview will capture photo and it will show it on that imageview then on second imageView will capture photo on next 2 second and show on this imageviewand so on....
I have already applied take picture by default camera by passing intent and it will open camera on another screen and capture photo will save in sdcard then we pic image and after resizing will save on that particular imageView so please don't tell me to apply this way. I don't want this. What i want i mentioned above.
UPDATE
I have used camera API for this task, following code i applied
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_cam);
pic = (ImageView)findViewById(R.id.imageView1);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
RelativeLayout preview = (RelativeLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
Here ShowCamera is my class where i have implements SurfaceHolder.Callback .and write all camera related operations.I have added camera on Relative layout. Its working fine but I want to open camera and perform all operations on ImageView. What tricks should i apply here to do this. Please help..
UPDATE
Can we open camera on multiple ImageView simultaneously like ios. Can we get different camera image frames or preview and set them on different ImageView. So that user can feel that multiple camera are opened simultaneously.
I am using this link
Fliping the Front Camera to Back Camera in Button Click using android
By this i am able to open camera and switch camera from back to front. But in this question i don't get the xml layout file. I am not getting that how to open camera on different imageView.
But by the same id how to handle it with different imageView.
If you are referring this tutorial, then follow the following steps
You need use a Timer for each two seconds call -
camera.takePicture(null, null,
new PhotoHandler(getApplicationContext()));.
Photo will be captured and you will get the callback on
#Override
public void onPictureTaken(byte[] data, Camera camera) {
//Set data to ImageView here
}
Since you need lot of ImageViews, it is better you use GridView and set photo to the items in onPictureTaken method.
It looks like in camera image capture, one can only capture either thumbnail or full image but not both in one pass because
public void startCamera() {
...
camera.putExtra("output", imageUri); (step 1)
...
needs to be declared before
...
startActivityForResult(camera, IMAGE_CAPTURE); (step 2)
...
Bundle extras = camera.getExtras();
mImageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(mImageBitmap);
...
But once "onActivityResult" returns, the full image is already saved into imageUri and the buffer cleared. But to capture the thumbnail of an image taken, the code needs to be executed after "startActivityForResult". The problem is the image buffer is cleared once the image is saved in step 2. To capture the image thumbnail, one will need to skip saving the full image in step 1 in order to capture the thumbnail image in step 2.
I can use an alternative to save the full image, reload the full image into bitmap, scale the image into a thumbnail size and resave the image but it seems to be redundant. Any idea if I can do both in one pass?
Check out MediaStore.Images.Thumbnails, and specifically getThumbnail (near the bottom): http://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html .
If that doesn't work, yes, you will have to manually re-scale and save the thumbnail yourself.