0

i want to take a photo with the android camera like this:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

this.startActivityForResult(intent, Globals.REQUEST_CODE_CAMERA)

And to store it in an ImageView:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == Globals.REQUEST_CODE_CAMERA) {
        if(resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");

            this.imageViewPhoto.setImageBitmap(bitmap);
        }
    }
}

My ImageView is configured like this:

<ImageView
        android:id="@+id/nfcresult_imageview_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="@string/imageview_photo_description" />

It all works but the photo shown on the ImageView is much smaller than the photo taken by the camera. What i want to do is to have a small preview in my ImageView and to add an OnClickListener to the ImageView to open up a Dialog which shows the original photo with the original size and resolution. It cant be that hard to do it but i actually cant find out how.

To create the Dialog and to show the photo i do this:

ImageView clone = new ImageView(this);
clone.setImageBitmap(((BitmapDrawable)this.imageViewPhoto.getDrawable()).getBitmap());

DialogManager.showImageDialog(this, this.getResources().getString(R.string.title_photo), clone);

The showImageDialog:

 public static void showImageDialog(Context context, String title, ImageView imageView) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder.setTitle(title);
    builder.setCancelable(false);
    builder.setView(imageView);
    builder.setPositiveButton(context.getResources().getString(R.string.button_back), new DialogInterface.OnClickListener() {
        /**
         * 
         */
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    builder.create().show();
}

The dialog now shows the photo with the size of the photo stored in the imageView but i want to show the original photo with original size and original resolution but as i already said, the ImageView should show a smaller version of the original photo.

How can i achieve that?

Mulgard
  • 9,877
  • 34
  • 129
  • 232

3 Answers3

0

instead of android:layout_width="wrap_content" set the imageview width and height to specific dp value

<ImageView
        android:id="@+id/nfcresult_imageview_photo"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:src="@drawable/ic_launcher" 
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="@string/imageview_photo_description" />

maintain the aspect ratio of height and width (instead of 100 and 200, choose right values)

Aun
  • 1,883
  • 1
  • 17
  • 26
0

You have to put an extra in your intent called "MediaStore.EXTRA_OUTPUT", where you have to set an uri for the picture to store to. After your callback function got called, you can load the Picture from the choosen uri. There you get the picture in full resolution.

MD1948
  • 292
  • 2
  • 13
0

The Solution:

/**
 * 
 */
private void performAddPhoto() {
    String timeStamp = Clock.getTimeStamp();
    String fileName = "Food_Shot_" + timeStamp + ".jpg";

    this.imagePath = Environment.getExternalStorageDirectory() + "/images/" + fileName;

    File file = new File(this.imagePath);
    file.getParentFile().mkdirs();

    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

    startActivityForResult(intent, Globals.REQUEST_CODE_CAMERA);
}

/**
 * 
 */
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == Globals.REQUEST_CODE_CAMERA) {
        if(resultCode == RESULT_OK) {
            this.onPhotoTaken();
        }
    }
}

/**
 * 
 */
private void onPhotoTaken() {
    this.imageTaken = true;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;

    Bitmap bitmap = BitmapFactory.decodeFile(this.imagePath, options);
    this.imageViewPhoto.setImageBitmap(bitmap);
}

/**
 * 
 */
private void performShowPhoto() {
    ImageView imageView = new ImageView(this);
    Bitmap bitmap = BitmapFactory.decodeFile(this.imagePath);

    imageView.setImageBitmap(bitmap);

    DialogManager.showImageDialog(this, this.getResources().getString(R.string.title_photo), imageView);
}
Mulgard
  • 9,877
  • 34
  • 129
  • 232