【问题标题】:How to save image PERMANENTLY after using camera to take photo in android?使用相机在android中拍照后如何永久保存图像?
【发布时间】:2020-04-28 14:52:43
【问题描述】:

我知道这已在此处多次发布,但没有一个解决方案足够清楚地说明需要做什么。我想知道我是 android 编程新手会有所帮助。

这是我在应用中创建使用相机的意图的地方。

public void captureImage(View view)
    {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(cameraIntent.resolveActivity(getPackageManager())!=null)
        {
            File imageFile = null;
            try {
                imageFile = getImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(imageFile!=null)
            {
                Uri imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, IMAGE_REQUEST);
            }

        }

    }

以下是我创建图像文件的方法,据我所知,相机图像将被临时保存。

    public File getImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmsss").format(new Date());
        String imageName = "jpg_"+timeStamp+"_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        //storageDir.mkdirs();
        perStorageDir=storageDir;
        File imageFile = File.createTempFile(imageName,".jpg", storageDir);
        currentImagePath = imageFile.getAbsolutePath();
        return imageFile;
    }

(使用currentImagePath我只能在应用程序仍在运行时访问照片)

  1. getExternalFilesDir(Environment.DIRECTORY_PICTURES); 如果我尝试在函数之外的任何位置调用它,我的应用程序就会崩溃,我不知道为什么。

  2. File.createTempFile(...) 仅创建一个临时文件。我怎样才能使它成为永久添加?

  3. 项目中的所有其他相关文件都已相应修改以设置权限和功能。

我要做的就是创建一个目录并在每次拍摄照片时将照片存储在其中。即使我关闭并重新打开应用程序或不拍照,我也想访问旧图像等的文件夹。

任何帮助将不胜感激!

【问题讨论】:

    标签: java android android-studio android-camera android-file


    【解决方案1】:

    如果您想永久保存它,您可以保存它App-Specific Storage。这是分配给您的应用程序包名称的一种存储类型。

    您可以在路径 data/data/your-package-name 中找到它,查看official documentation 以了解有关此类存储的更多信息。

    我们也应该将这些图像保存在外部存储中。

    第 1 步:

    授予外部存储权限,因为您将通过在其中保存图像来访问此存储。

    第 2 步:

    使用 Intent 打开您的相机并为拍摄的图像创建一个文件。

        private void openCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            photoFile = null;
            try {
                photoFile = FileHelper.createImageFile(this);
    
            } catch (IOException ex) {
            }
    
            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(this,
                        "your path",
                        photoFile);
                deleteFile(photoFile.getName());
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, CAMERA_RESULT);
                photoFile.deleteOnExit();
            }
        }
    }
    

    createImageFile() 是一种创建临时文件的方法

       public static File createImageFile(Context context) throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = timeStamp;
        File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        ;
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir     /* directory */
        );
        return image;
    }
    

    第 3 步:

    添加文件提供者的路径

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path
        name="external"
        path="." />
    </paths>
    

    在您的清单中,如下声明此文件提供程序路径:

    <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="cyour authority"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    

    最后,我在GitHub 上有一个完整的存储库,你可以探索它,我认为它可以帮助你。

    快乐编码?

    【讨论】:

    • 我很抱歉,但我无法理解这段代码与我的代码有何不同......除了上下文参数之外,我相信它们基本上都在做同样的工作。我仍然不明白如何创建一个可以随时访问的永久目录路径,即使不点击照片也是如此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    相关资源
    最近更新 更多