7

I am using the jengelsma code from this post Capture Image from Camera and Display in Activity to get an image from the camera.

When I get the returned image it is only small and when I try to expand it I instantly lose the image quality.

I assume that this is only the preview size however I have been unable to find out to return the large image as it is this I need in order to manipulate it.

For reference the code is:

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 
}

Any help greatly appreciated.

Community
  • 1
  • 1
EHarpham
  • 602
  • 1
  • 17
  • 34

2 Answers2

19

You have to do a bit extra to get a fullsize picture, it doesn't pass it in the Intent, but stores it in a file.

To start the camera for this, add an extra to your Intent:

Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );

fileUri = getOutputMediaFileUri();
intent.putExtra( MediaStore.EXTRA_OUTPUT, fileUri );

getOutputMediaFileUri looks like:

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(){
  return Uri.fromFile(getOutputMediaFile());
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
          Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");

    return mediaFile;
}

Naturally you can modify the method that creates the file however you like to modify the naming.

Then in your onActivityResult you can get the URI to the picture by simply calling getData() on the Intent.

Khantahr
  • 8,156
  • 4
  • 37
  • 60
  • Thanks for your help. I have used your code example however this is not writing the image to anywhere on my device (S2). I have searched the device for the timestamp with no results. Will this differ between devices? or does this not save it to the device? – EHarpham Oct 21 '12 at 19:57
  • On onActivityResult my data is null :( And I can't use getData(). – yAnTar Aug 04 '13 at 22:49
  • @yAnTar did you find out why? Because the picture is stored on the SD, but my data=null. And getData() gives nullpointer.. – Diego Dec 04 '13 at 04:54
  • Not all cameras returns the URI – iflorit May 02 '14 at 07:12
  • 1
    @Ralgha Nice code. I want to reduce the size of this image before saving in sd card. How can I do it? – Nitesh Kumar May 27 '15 at 07:07
  • @Ralgha i want to compress the caotured image using this method how can i achieve that ? – Kaustubh Bhagwat Dec 06 '17 at 10:06
2

start intent for camera

private void takeFromCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            File exportDir = new File(Environment.getExternalStorageDirectory(), "TempFolder");
            if (!exportDir.exists()) {
                exportDir.mkdirs();
            }

            String fileName = "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
            mTempCameraPhotoFile = new File(exportDir,fileName);
            Log.d(LOG_TAG, fileName);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempCameraPhotoFile));
            startActivityForResult(takePictureIntent, REQUEST_CAMERA);
        }
    }

capture result in onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {

        if  (requestCode == REQUEST_CAMERA) {


            if (requestCode == REQUEST_CAMERA) {
                String filePath = mTempCameraPhotoFile.getPath();
                Log.d(LOG_TAG, filePath);

                /*
                // extracted file path can be load into imageview for example with Picasso loader
                Uri uri = Uri.fromFile(new File(mReportImageList.get(position).getUrl()));
                Picasso.with(mContext)
                        .load(uri)
                        .fit()
                        .centerCrop()
                        .into(ivPhoto);*/


            }
        }
    }
}    

in manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Seyyed
  • 1,526
  • 3
  • 20
  • 30
Tigran Sarkisian
  • 1,088
  • 10
  • 13
  • `Log.d(LOG_TAG, "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");` generates wrong result. because you are calling ` UUID.randomUUID()` again and it generates new UUID not previous UUID – Seyyed Nov 15 '19 at 18:42