如何在Android中使用相机拍照后永久保存图像?

问题描述 投票:0回答:1

我知道这已经多次发布在这里,但是对于需要做什么,没有一个解决方案足够清楚。我想知道我是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个回答
0
投票

如果要永久保存,可以将其保存为[[特定于应用程序的存储]。这是分配给您的应用程序包名称的存储类型。

您可以在路径数据/数据/

您的包名称]中找到它,请检查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上拥有完整的存储库,您可以浏览它,我认为它可以为您提供帮助。

快乐编码🤓

© www.soinside.com 2019 - 2024. All rights reserved.