如何将图片保存在特定文件夹中仅用于我的应用程序(正确)? [英] How to save a picture in a specific folder just for my app (correctly)?

查看:33
本文介绍了如何将图片保存在特定文件夹中仅用于我的应用程序(正确)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我的应用保存了两次图片;一个在相机文件夹中,另一个在我指定的文件夹中.但是当我在另一台设备上测试该应用程序时,这并没有发生!

The problem is that my app saves the picture twice; one in the camera folder and the other in the folder I specified. but when I tested the app on another device, that didn't happen!

//lunch the camera and make a file to save the image in and pass it with the camera intent
    public void lunchCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.ziad.sayit",
                        photoFile);

                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "SayIt_" + timeStamp + "_";
        File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File imageFile = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = imageFile.getAbsolutePath();
        return imageFile;
    }

所以,我想要一个解决方案,我还想将图片保存在我的应用里面图片目录的文件夹中..谢谢

So, I want a solution for this, and I also want to save the picture in a folder for my app inside the Pictures Directory.. thanks

推荐答案

除了不使用 ACTION_IMAGE_CAPTURE 之外,没有其他解决方案.

There is no solution for it, other than to not use ACTION_IMAGE_CAPTURE.

ACTION_IMAGE_CAPTURE 将拍照委托给任意第三方相机应用.设备上预装了数十种(如果不是数百种).Play 商店和其他地方还有数百种可供下载.他们如何响应 Intent 行动取决于他们.理想情况下,他们只会将图像存储在EXTRA_OUTPUT 指定的位置.但是,并不要求他们以这种方式行事.一些相机应用程序会将图像存储两次,一次在其正常位置,一次在 EXTRA_OUTPUT 中.有些人会完全忽略 EXTRA_OUTPUT.

ACTION_IMAGE_CAPTURE delegates picture-taking to an arbitrary third-party camera app. There are dozens, if not hundreds, of these pre-installed on devices. There are hundreds more available for download from the Play Store and elsewhere. What they do in response to that Intent action is up to them. Ideally, they would only store the image in the location specified by EXTRA_OUTPUT. However, there is no requirement that they behave that way. Some camera apps will store the image twice, once in its normal location and once in the EXTRA_OUTPUT. Some will ignore EXTRA_OUTPUT entirely.

如果这与您有关,请不要使用 ACTION_IMAGE_CAPTURE.使用库CameraX、Fotoappart、CameraKit-Android 等—在您自己的应用中拍摄照片.

If this concerns you, do not use ACTION_IMAGE_CAPTURE. Use a library — CameraX, Fotoappart, CameraKit-Android, etc. — to take the photos within your own app.

这篇关于如何将图片保存在特定文件夹中仅用于我的应用程序(正确)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆