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

问题描述 投票:0回答:1

[尝试捕获图像并将该图像保存到图库中。但是,我似乎有一些错误。我尝试遵循以下文档: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);
    }

Android清单:

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

感谢您提前提供帮助!

java android android-studio android-camera filepath
1个回答
0
投票

您的实现是错误的,应该是这样

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

元数据应在清单中的提供者内部,这就是为什么Android无法接收提供者的原因。

© www.soinside.com 2019 - 2024. All rights reserved.