Garren Fitzenreiter
Garren Fitzenreiter

Reputation: 839

I want to open the android camera without saving the picture to gallery

Screenshot

I want the picture to go straight to the ImageView without saving it to gallery if possible. As shown in the screenshot, it will ask to save everytime and will save straight to the gallery. Can this be achieved, or will I have to make my own ImageView camera?

public class Main extends Activity {

ImageView ivPhoto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ivPhoto = (ImageView) findViewById(R.id.ivPic);
}

public void TakePhoto(View v){
    Intent camIntent = new      Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camIntent,0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==0){
        Bitmap camImage = (Bitmap) data.getExtras().get("data");
        ivPhoto.setImageBitmap(camImage);
    }
}

Upvotes: 2

Views: 6791

Answers (4)

Garren Fitzenreiter
Garren Fitzenreiter

Reputation: 839

Finally got what I wanted. Thanks guys

public class Main extends Activity {

ImageView ivPhoto;
File myFilesDir;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ivPhoto = (ImageView) findViewById(R.id.ivPic);
    myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.example.project/files");
    System.out.println (myFilesDir);
    myFilesDir.mkdirs();
}

public void TakePhoto(View v){
    Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    camIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(myFilesDir.toString()+"/temp.jpg")));
    startActivityForResult(camIntent, 0);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==0){
        try {
            Bitmap cameraBitmap;
            cameraBitmap = BitmapFactory.decodeFile(myFilesDir + "/temp.jpg");
            Bitmap.createBitmap(cameraBitmap);
            ivPhoto.setImageBitmap(cameraBitmap);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}


}

Upvotes: 6

FabianCook
FabianCook

Reputation: 20587

From my understanding you don't want this to be showing up by any media scanner, like the gallery application. What you should actually do is not store it in a root directory like pictures or sdcard, but store it in your applications data folder in the sdcard in Android/data/package/.

You can get this using: http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

File myFilesDir = getExternalFilesDir(null);

OR

File myFilesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

Note that it will only work on API versions 8 or above.

If you don't want to use the function you can simply just use:

File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + packageName + "/files");
myFilesDir.mkdirs();

Upvotes: 2

Rahul Gupta
Rahul Gupta

Reputation: 5295

Use my Code. I am taking a picture using camera intent and before saving it to gallery , it is showed to the user with a Save and Cancel Button :- Call Camera Intent :-

                    String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator +CommonFunction.getDateTime()+".jpg"; // Get File Path
                    Intent takePictureFromCameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    takePictureFromCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
                    startActivityForResult(takePictureFromCameraIntent, 123);

onActivityResult : -

public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_RESULT) 
        {
            if (resultCode == Activity.RESULT_OK) 
            {
                String galleryImatePath = SD_CARD_TEMP_DIR; // make SD_CARD_TEMP_DIR Global so that you can access it here from camera intent or pass it in put Extra method and retrieve it here

                File f = new File(galleryImatePath);

                try {
                            Bitmap cameraBitmap = null;
                            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                            bmOptions.inJustDecodeBounds = false;
                            bmOptions.inPurgeable = true;
                            bmOptions.inBitmap = cameraBitmap; 
                            bmOptions.inMutable = true; 


                            cameraBitmap = BitmapFactory.decodeFile(galleryImatePath,bmOptions);
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);

                            //To Rotate image Code
                                ExifInterface exif = new ExifInterface(galleryImatePath);
                                float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
                                System.out.println(rotation);

                            float rotationInDegrees = exifToDegrees(rotation);
                            System.out.println(rotationInDegrees);

                            Matrix matrix = new Matrix();
                            matrix.postRotate(rotationInDegrees);

                            final Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true);
                            FileOutputStream fos=new FileOutputStream(galleryImatePath);
                            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
                            fos.write(bos.toByteArray());
                            cameraBitmap.recycle();
                            System.gc();
                            fos.flush();
                            fos.close();


                            // To set image in imageview in dialog
                        Capdialog = new Dialog(AddToDo.this,android.R.style.Theme_NoTitleBar_Fullscreen);
                        Capdialog.setContentView(R.layout.captiondialog);
                        Capdialog.setCancelable(false);
                        TextView cancel = (TextView) Capdialog
                                .findViewById(R.id.cancel);
                        TextView done = (TextView) Capdialog.findViewById(R.id.done);
                                                    Capdialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                        ImageView img = (ImageView) Capdialog.findViewById(R.id.image);
                        img.setImageBitmap(rotatedBitmap);
                   }
                   catch(Exception e){}
          }
     }
}

implement your done and cancel on click listener - what you want to do in them. My code will capture your image, rotate it in the right direction irrespective of camera rotation and show it to you in a dialog before saving it

Upvotes: 2

Morrison Chang
Morrison Chang

Reputation: 12121

Google provided a tutorial on this exact topic: Controlling the Camera

Upvotes: 1

Related Questions