0

How do I fit an image to the screen captured from the camera? I currently have a preview that is a sort of thumbnail version of the image taken. I want to touch it and fit it to the whole screen. In the layout, I have the property:

android:onClick="previewPhoto"

But I don't know how to proceed. Here is what I think I should do, but wonder if there is a simpler way. If I go with starting a new activity, how should I finish the following code?

public void previewPhoto(View view){
    Toast.makeText(this, "Photo touched!", Toast.LENGTH_SHORT).show();
    mImageView.setScaleType(ScaleType.FIT_XY);

    Intent fullScreenIntent = new Intent(view.getContext(),FullImageActivity.class);
    fullScreenIntent.putExtra
    //ProfilePageNormalUser.this.startActivity(fullScreenIntent); 
}

Note that mImageView is an object that holds the photo captured. When I simply use setScaleType(ScaleType.FIT_XY); it will fit the image to the tiny thumbnail view.

sAguinaga
  • 638
  • 13
  • 31

2 Answers2

0

3 possible solutions , but for all of them , you need to set a new layoutParams for the view (for both width and height , set them to fillParent ) :

  1. use "setContentView" on the imageView to use it as the new layout . when done , go back to the previous layout by using this method on the layout that you used .

  2. it needs to take over the entire space of the root view . this solution depends on the layout of the activity . for example , if the layout is complex , wrap it in a framelayout , and move the view to the framelayout and then set the layoutParams.

  3. set the view to be on top of all of the views , and then set the layoutParams.example (in the example , the "view" is the one that you wish to set on top , for you , this would be "mImageView") :

...
final View view=findViewById(R.id.view1);
final WindowManager.LayoutParams layoutParams=new WindowManager.LayoutParams();
layoutParams.gravity=Gravity.TOP|Gravity.LEFT;
layoutParams.width=view.getLayoutParams().width;
layoutParams.height=view.getLayoutParams().height;
layoutParams.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.flags|=WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
final ViewGroup parent=(ViewGroup)view.getParent();
parent.removeView(view);
final WindowManager windowManager=(WindowManager)getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(view,layoutParams);
// TODO handle overlapping title bar and/or action bar if needed
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • The ImageView is currently a child widget of a my main layout, what I mean is multiple items are being displayed + the newly taken photo so can I programmatically create a new View that is not in my layout xml file? My goal is to fit it to screen, then ask user if the photo should be saved to storage or discarded. – sAguinaga Jun 02 '12 at 09:54
  • Because mImageView is a thumbnail, the code you suggest is not fitting the image to the screen. The photo stays thumbnail size with blank space above and below it and cropped to its width. – sAguinaga Jun 02 '12 at 10:27
  • well did it expand to either the width or height of the device ? if so, that's because it kept the aspect ratio in order to still make it look good . if you don't want it to keep the aspect ratio , set "adjustViewBounds" to false . about the question of using a view without xml , of course you can . you can even use "setContentView" on an actual view , even after your activity already has shown a layout before . i've updated my answer to include this solution. – android developer Jun 02 '12 at 12:55
  • anyway , please show the relevant code so that i could tell you what's wrong. – android developer Jun 02 '12 at 16:31
0

If you just need to show the photo in full screen and not custom layout/buttons are required, you can launch the gallery app (or any other 3rd party apps with support for displaying images) with the following code:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + picturePath),"image/*");
startActivity(intent);  

Edit:

picturePath can be set when launching the camera, so you know where the image is stored. Please have a look to the method takePhotoFromCamera here:

https://github.com/miguelrodelas/Android-Snippets/blob/master/ImageSnippets.java

This method returns the picture path. It uses the method getOutputMediaFile (also there) to generate an image name from the date.

MRD
  • 7,146
  • 1
  • 21
  • 21
  • If I use this code, but I haven't saved the image to storage (I am holding the image in the ImageView widget) how does the `Uri.parse("file://" + picturePath)` argument need to be handled? In place of it, can I just pass the mImageView object or the bitmap object? – sAguinaga Jun 02 '12 at 09:47
  • I have edited my answer to include how to know the picture path. I think you are using the thumbnail you get onActivityResult. To show the image in full screen, you will need to know the picture path, so please try the method above. – MRD Jun 02 '12 at 14:19