【问题标题】:Android camera intent should not allow to save the image to gallery and store it in specified pathAndroid相机意图不应允许将图像保存到图库并将其存储在指定路径中
【发布时间】:2016-06-14 21:04:00
【问题描述】:

当我使用此代码时 ->

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

我可以将图像保存到指定路径,但它也会保存到图库。我不想将图像保存到画廊。请在这里帮忙。

提前感谢您的宝贵时间。

【问题讨论】:

标签: android android-intent android-camera-intent


【解决方案1】:

试试这个

private void captureCameraImage() {

        Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFilename());

        startActivityForResult(chooserIntent, CAMERA_PHOTO);

    }

返回文件名的方法,您可以指定要保存的位置

public String getFilename() {
    File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyFolder/Images");
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
    return uriSting;

}

【讨论】:

    【解决方案2】:

    使用以下 Intent 捕获图像 -

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    
    // create a file to save the image
    File MyDir = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir");
    if (!MyDir.exists()){
        MyDir.mkdirs()
    }
    
    File fileUri = new File(MyDir.getAbsolutePath() + File.separator + "IMG_"+ timeStamp + ".jpg"); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    
    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    

    图库应用扫描文件夹中的媒体内容并将它们填充到图库中。

    如果您不想在图库中显示您捕获的图像,请按照以下方法-

    1. 创建 .Nomedia 文件

    2. 添加点前缀

    【讨论】:

    • 嗨..当我签入文件管理器时,我发现相同的图像存储在两个文件夹 1)DCIM 文件夹 2)我指定的 URI
    • @UmakantAngadi:检查我的编辑。我添加了从相机 Intent 捕获图像的代码。希望对您有所帮助。
    • 感谢您的代码。在这里我面临问题 1)它仍然将该图像保存到 /storage/emulated/0/DCIM/Camera/20160302_143206.jpg 2)我将该图像保存在 /storage/emulated/0/sample/IMG_20160302_143158.png 中图像在 2 个具有不同文件名的文件夹中。我不认为手机正在扫描文件夹并创建具有不同文件名的相同图像。我的问题是:手机默认创建图像并存储到 DCIM 文件夹(图库)中。这不应该发生。提前致谢。
    • @UmakantAngadi:绝对不是。它不会在 DCIM 中扫描和创建重复文件。它只是将您文件夹中的图像显示到图库中
    • @UmakantAngadi:嘿,你可以查看这个链接stackoverflow.com/questions/8078892/…
    【解决方案3】:

    我遇到了同样的问题并实施了几个类似于one 的解决方法。 我还尝试隐藏文件,将 . 前缀添加到 filname 并将 .nomedia 文件(请参阅MediaStore.MEDIA_IGNORE_FILENAME)放在我存储图像的文件夹中,但在某些情况下,通过意图调用相机应用程序平常的

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
        Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, CAMERA_REQUEST_CODE);
    } else {
        showToastMessage(getString(R.string.no_camera_activity), Toast.LENGTH_LONG);
    }
    

    取决于设备和相机应用程序,即使您提供与您正在存储的文件相关联的Uri,后者也可能将图片存储在图库中(通常保存带有时间戳作为文件名的文件)在您的应用程序私有分区中。

    所以我发现做我需要的最可靠的方法是你自己直接control the camera或通过这种方式在YourActivity中采用CommonsWare提供的cwac-cam2库(注意注释@987654330 @线)

    Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
    CameraActivity.IntentBuilder builder = new CameraActivity.IntentBuilder(this); // this refers to the activity instance
        Intent intent = builder
                .skipConfirm()
                .facing(Facing.BACK)
                .to(fileUri)
                //.updateMediaStore() // uncomment only if you want to update MediaStore
                .flashMode(FlashMode.AUTO)
                .build();
        startActivityForResult(intent, CAMERA_REQUEST_CODE);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多