简体   繁体   English

相机意图-如何?

[英]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. 很抱歉打扰你们,但是我无法在Intent中使用意图拍照时得到解决方案。 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; } 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. 数百种相机应用程序可能会响应ACTION_IMAGE_CAPTURE ,并且这些应用程序的开发人员可以执行所需的任何操作。

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). 使用相机API或环绕它们的库(例如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 . 看看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. 最终,在过去2天的等待之后,我找到了答案,是的。它不会保存文件,因为我只是从活动返回后删除文件。

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

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

int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToLast(); 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))); sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(f)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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