简体   繁体   中英

Camera Intent - how to?

Sorry to bother you guys, but I am not able to get a solution where In we take picture using intents. I know the default behavior of native camera is to save the picture at default directory/place of OS The thing is I have some requirements where I do not want to save the picture when clicked using camera app. There has to be a solution of this issue, be it like once we take a picture we could delete it right away, or there should be an alternate by which we won't allow OS to save Image, please help.

Here is a piece of code I tried, tried several ways by creating a directory and then deleting file, nothing works.

 public void takeImageFromCamera() {

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }


  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// Check for the integer request code originally supplied to startResolutionForResult().
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

            if (isCameraPermissionGranted()) {
                bitmap= (Bitmap) data.getExtras().get("data");
              //  bitmap = processReuiredImage(picUri);
                getProfileDetailViaFace(encodeImageBitmapToString(bitmap));
                Log.d("path",String.valueOf(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES)));

              //  getApplicationContext().getContentResolver().delete(, "/storage/emulated/0/Pictures", null);
            //     mediaStorageDir.getPath().delete();


            } else {
                requestCameraPermission();
            }
        }

public void takeImageFromCamera() {

    File file = getOutputMediaFile(CAMERA_FILE_TYPE);

    if (Build.VERSION.SDK_INT >= 24) {
        try {

            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    picUri = Uri.fromFile(file);

    Intent takePictureIntent = new 
   Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, CAMERA_REQUEST);
    }
    }

   private File getOutputMediaFile(int type) {
    mediaStorageDir  = new 
     File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "peppercard");

    /**Create the storage directory if it does not exist*/
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    /**Create a media file name*/
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    if (type == CAMERA_FILE_TYPE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_" + timeStamp + ".jpeg");
    } else {
        return null;
    }

} return mediaFile;

}

The thing is I have some requirements where I do not want to save the picture when clicked using camera app

The decision of whether or not to save an image is up to the camera app, not you. There are hundreds of camera apps that might respond to ACTION_IMAGE_CAPTURE , and the developers of those apps can do whatever they want.

There has to be a solution of this issue, be it like once we take a picture we could delete it right away, or there should be an alternate by which we won't allow OS to save Image,

Take the photo yourself, using the camera APIs or libraries that wrap around them (eg, CameraKit-Android, Fotoapparat).

There has to be a solution of this issue, be it like once we take  
a picture we could delete it right away

Indeed there is. You could specify a path (even using a file provider) where the camera app has to put the image in a file.

Then when the camera app is done you can get the image from that file and then delete the file.

Have a look at Intent.EXTRA_OUTPUT .

Pretty standard your question. You can find a lot of example code on this site.

Final I have found the answer after waiting from past 2 days, yay..It will not save the file as I am just deleting the file after returning from the activity.

String[] projection = { MediaStore.Images.Media.DATA };

Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null)

int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToLast();

                    String imagePath = cursor.getString(column_index_data);
                    Bitmap bitmapImage = BitmapFactory.decodeFile(imagePath );
                    Log.d("bitmapImage", bitmapImage.toString());                       /*delete file after taking picture*/
                    Log.d("imagePath", imagePath.toString());
                    File f = new File(imagePath);
                    if (f.exists()){
                        f.delete();

                    }

sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(f)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM