java - 将照片保存到图库时出错 - Android Studio (java)

标签 java android android-studio android-camera filepath

try catch 图像并将该图像保存到图库中。但是,我似乎有一些错误。我尝试按照以下文档进行操作: https://developer.android.com/training/camera/photobasics

但似乎 android.support.v4.content.FileProvider 已经过时了......?

除此之外,我的文件路径导致 fatal error :

    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) 

下面是我调用 Intent 打开相机并尝试保存拍摄的图像的地方:

    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);
    }

Android list :

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

元数据应该在 list 中的提供程序内,这就是为什么android无法接收提供程序。

同时将 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>

关于java - 将照片保存到图库时出错 - Android Studio (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58256206/

相关文章:

java - 最简单的客户端-服务器中的连接重置异常

javascript - 使用一个方法作为另一个方法的参数 *Java Processing*

java - 在 Firebase 实时数据库和经过身份验证的用户之间创建链接

java - Android - 全局变量 - 应用程序错误

android - 如何在 android studio 中使用 Jacoco 为 androidTest 生成代码覆盖率报告

java - 如何使用JDBC查看数据库的具体行数

java - 无法使用 CAdES 方法签署 PDF,尽管使用 PAdES 签署成功

Android - 连接成功后无法在 onScanResult 回调中找到设备

java - "INSTALL_PARSE_FAILED_MANIFEST_MALFORMED"在Android Studio中运行项目时出错

java - :app:dexDebug ExecException finished with non-zero exit value 2