Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top