gpt4 book ai didi

java - 使用相机拍照并保存在 FileProvider 中

转载 作者:行者123 更新时间:2023-12-01 21:24:02 25 4
gpt4 key购买 nike

我需要有关使用 FileProvider 在 Android(Java 中)中拍照的帮助。我写了关于拍照的 Android 教程(+更多),所以我最终遇到了以下情况:我的 list :

    <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>

我的 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>

然后,调用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();
}
}

最后,onActivityResult:

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

问题在于,在startCamera方法中,第14行:

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

我有一个 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)

最佳答案

删除
路径=“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>

关于java - 使用相机拍照并保存在 FileProvider 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58839379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com