将照片保存到gallery Android Studio(java)时出错
正在尝试捕获图像并将该图像保存到库中。然而,我似乎有一些错误。我尝试了以下文档: https://developer.安卓.com/training/camera/photobasics
但它似乎已经过时了
除此之外,“我的文件路径”还会导致一个致命错误:
Process: com.example.studyshots, PID: 9863
java.lang.RuntimeException: Unable to get provider 安卓x.core.content.FileProvider: java.lang.IllegalArgumentException: Missing 安卓.support.FILE_PROVIDER_PATHS meta-data
at 安卓.app.ActivityThread.installProvider(ActivityThread.java:6683)
at 安卓.app.ActivityThread.installContentProviders(ActivityThread.java:6225)
at 安卓.app.ActivityThread.handleBindApplication(ActivityThread.java:6140)
at 安卓.app.ActivityThread.access$1200(ActivityThread.java:235)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at 安卓.os.Handler.dispatchMessage(Handler.java:106)
at 安卓.os.Looper.loop(Looper.java:214)
at 安卓.app.ActivityThread.main(ActivityThread.java:6986)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.IllegalArgumentException: Missing 安卓.support.FILE_PROVIDER_PATHS meta-data
at 安卓x.core.content.FileProvider.parsePathStrategy(FileProvider.java:608)
at 安卓x.core.content.FileProvider.getPathStrategy(FileProvider.java:579)
at 安卓x.core.content.FileProvider.attachInfo(FileProvider.java:392)
at 安卓.app.ActivityThread.installProvider(ActivityThread.java:6678)
at 安卓.app.ActivityThread.installContentProviders(ActivityThread.java:6225)
at 安卓.app.ActivityThread.handleBindApplication(ActivityThread.java:6140)
at 安卓.app.ActivityThread.access$1200(ActivityThread.java:235)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at 安卓.os.Handler.dispatchMessage(Handler.java:106)
at 安卓.os.Looper.loop(Looper.java:214)
at 安卓.app.ActivityThread.main(ActivityThread.java:6986)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.安卓.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.安卓.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);
}
Android清单:
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
package="com.example.studyshots">
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:roundIcon="@mipmap/ic_launcher_round"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity 安卓:name=".photo_view_activity" />
<activity 安卓:name=".AlbumView" />
<activity 安卓:name=".SettingsActivity" />
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
安卓:name="安卓.support.FILE_PROVIDER_PATHS"
安卓:resource="@xml/file_paths" />
<provider
安卓:name="安卓x.core.content.FileProvider"
安卓:authorities="com.example.安卓.fileprovider"
安卓:exported="false"
安卓:grantUriPermissions="true" />
</application>
<uses-feature
安卓:name="安卓.hardware.camera"
安卓:required="true" />
<uses-permission 安卓:name="安卓.permission.READ_EXTERNAL_STORAGE" />>
<uses-permission 安卓:name="安卓.permission.CAMERA" />
<uses-permission 安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
最后是我的文件路径。xml:
<?xml version="1.0" encoding="utf-8"?>
<MainActivity xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
<paths>
<external-files-path name="my_images" />
</paths>
</MainActivity>
提前谢谢你的帮助
# 1 楼答案
您的实现是错误的,应该是这样的
元数据应该在清单中的提供者内部,这就是为什么android无法接收提供者的原因
同时更新文件路径。xml作为