首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将照片保存到图库时出错- Android Studio (java)

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

Stack Overflow用户
提问于 2019-10-06 18:05:56
回答 1查看 78关注 0票数 0

尝试捕获图像并将该图像保存到图库。然而,我似乎有一些错误。我尝试遵循上的文档:https://developer.android.com/training/camera/photobasics

但是看起来android.support.v4.content.FileProvider已经过时了...?

除此之外,我的文件路径会导致一个致命错误:

代码语言:javascript
运行
复制
    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) 

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

代码语言:javascript
运行
复制
    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清单:

代码语言:javascript
运行
复制
<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:

代码语言:javascript
运行
复制
<?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>

提前感谢大家的帮助!

EN

回答 1

Stack Overflow用户

发布于 2019-10-07 00:10:13

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

代码语言:javascript
运行
复制
<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中应该包含提供者内部的元数据,这就是android无法接收提供者的原因。

还要将file_path.xml更新为

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="some-path" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58256206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档