java - 将照片保存到图库时出错 - Android Studio (java)
问题描述
尝试捕获图像并将该图像保存到图库。但是,我似乎有一些错误。我尝试遵循以下文档: https ://developer.android.com/training/camera/photobasics
然而它似乎android.support.v4.content.FileProvider
已经过时了......?
除此之外,我的文件路径会导致致命错误:
Process: com.example.studyshots, PID: 9863
java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data
at android.app.ActivityThread.installProvider(ActivityThread.java:6683)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:6225)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6140)
at android.app.ActivityThread.access$1200(ActivityThread.java:235)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6986)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data
at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:608)
at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579)
at androidx.core.content.FileProvider.attachInfo(FileProvider.java:392)
at android.app.ActivityThread.installProvider(ActivityThread.java:6678)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:6225)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6140)
at android.app.ActivityThread.access$1200(ActivityThread.java:235)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6986)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
以下是我调用意图打开相机并尝试保存拍摄的图像的地方:
private void dispatchTakePictureIntent() {
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
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
//Saving the full size image
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
//Add image to gallery
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
安卓清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.studyshots">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".photo_view_activity" />
<activity android:name=".AlbumView" />
<activity android:name=".SettingsActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true" />
</application>
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
最后是我的 File_paths.xml :
<?xml version="1.0" encoding="utf-8"?>
<MainActivity xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-files-path name="my_images" />
</paths>
</MainActivity>
提前感谢您的帮助!
解决方案
你的实现是错误的,应该是这样的
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="myPackageName.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
元数据应该在manifest中的provider中,这就是android无法接收provider的原因。
还将 file_path.xml 更新为
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="some-path" name="files_root" />
<external-path path="." name="external_storage_root" />
</paths>
推荐阅读
- passport-local-mongoose - 尽管遵循了文件,为什么我的护照身份验证代码会给出推荐信?
- visual-studio - 如何将 Visual Studio 主题和扩展复制到离线设备
- python-3.x - 如何创建 Python Wheel 或确定 Python Wheel 中包含哪些模块/库
- docker - 是否可以在 citrix VDI 上安装 docker 桌面
- flutter - BottomNavigationBarItem 中的多行标题
- c# - 如何将 JsonProperty 属性设置为我们无法控制的属性?
- indexing - Solr-8.9.0 索引目录大小减小
- asp.net-core - 在 Swagger UI 上默认为最新的 API 版本
- git - Git 下载/克隆 repo 中确定的/特定的文件夹
- openstack - 如何在 libvirt 中获取 kvm 实际 CPU 使用百分比?