简体   繁体   English

用相机拍照并保存在 FileProvider 中

[英]Take photo with Camera and save in FileProvider

I need help on taking a Photo in Android (in Java) using a FileProvider.我需要使用 FileProvider 在 Android(Java 中)中拍照的帮助。 I wrote the Android tutorial (+ much more) on taking photos, so I ended up with the following situation: My manifest:我写了关于拍照的 Android 教程(还有更多),所以我最终遇到了以下情况: 我的清单:

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

My res/xml/file_paths.xml:我的 res/xml/file_paths.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-files-path
            name="schede_images"
            path="Android/data/com.android.example/files/Pictures" />
    </paths>

Then, the method for callying the Camera Intent:然后,调用Camera Intent的方法:

    String currentPhotoPath;
    @OnClick(R.id.startCamera)
    public void startCamera(View v) {
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "provaprovaprova");
        if (!imagesFolder.exists()) {
            boolean isCreated = imagesFolder.mkdirs();
            if (!isCreated) {
                Toast.makeText(this, "Errore storage", Toast.LENGTH_LONG).show();
                return;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "IMG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        try {
            File image = File.createTempFile(imageFileName, ".jpg", storageDir);
            Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".fileprovider", image);
            currentPhotoPath = image.getAbsolutePath();
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, Util.getResources().getInteger(R.integer.ACTION_FOTO_CAPTURE));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Finally, the onActivityResult:最后,onActivityResult:

if (requestCode == Util.getResources().getInteger(R.integer.ACTION_FOTO_CAPTURE)) {
    doSomething();
}

The problem is that, in the method startCamera, on the 14th line:问题是,在 startCamera 方法中,第 14 行:

    Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".fileprovider", image);

I have an IllegalArgumentException:我有一个 IllegalArgumentException:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android.example, PID: 25184
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/sdcard/Android/data/com.android.example/files/Pictures/IMG_20191113_152356_376811633.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)

Remove消除
path = "Android/data/com.android.example/files/Pictures" to path = "/"路径=“Android/data/com.android.example/files/Pictures”到路径=“/”

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
     <external-files-path
        name="schede_images" path="/" />
</paths>

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

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