API上的XML相机意图上的空对象=> 24 [重复]

Posted

技术标签:

【中文标题】API上的XML相机意图上的空对象=> 24 [重复]【英文标题】:Null Object on XML Camera Intent on API =>24 [duplicate] 【发布时间】:2018-02-24 15:40:17 【问题描述】:

我试图让相机意图在更高的 API 上工作,但当我尝试时它崩溃了。我认为某些路径有问题,但我不知道是什么。它在 API 23 上运行良好。

FATAL EXCEPTION: main
                                                                    Process: ur.mi.android.wgplus05, PID: 22088
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
                                                                        at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
                                                                        at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
                                                                        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
                                                                        at ur.mi.android.wgplus05.Camera.takePicture(Camera.java:50)
                                                                        at ur.mi.android.wgplus05.PictureActivity.takePicture(PictureActivity.java:148)
                                                                        at ur.mi.android.wgplus05.PictureActivity.checkPermission(PictureActivity.java:140)
                                                                        at ur.mi.android.wgplus05.PictureActivity.onOptionsItemSelected(PictureActivity.java:101)
                                                                        at android.app.Activity.onMenuItemSelected(Activity.java:3204)
                                                                        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
                                                                        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                                                                        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
                                                                        at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:679)
                                                                        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:959)
                                                                        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:623)
                                                                        at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:154)
                                                                        at android.view.View.performClick(View.java:5610)
                                                                        at android.view.View$PerformClick.run(View.java:22260)
                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

第 50 行:

   Uri photoURI = FileProvider.getUriForFile(activityContext,
                    BuildConfig.APPLICATION_ID + ".genericfileprovider",
                    photoFile);


  public class Camera 
PictureActivity activityContext;
private String currentPhotoPath;

public Camera(PictureActivity activityContext) 
    this.activityContext = activityContext;


public void takePicture(int requestCode) 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(activityContext.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
            return;
        
        // Continue only if the File was successfully created

        if (photoFile != null) 
            if(Build.VERSION.SDK_INT < 24)
            Uri photoURI = Uri.fromFile(photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            activityContext.startActivityForResult(takePictureIntent, requestCode);
        
        else 
            Uri photoURI = FileProvider.getUriForFile(activityContext,
                    BuildConfig.APPLICATION_ID + ".genericfileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            activityContext.startActivityForResult(takePictureIntent, requestCode);
        
        
    


public String getCurrentPhotoPath() 
    return currentPhotoPath;


public Bitmap getScaledBitmap(String path, Point targetSize) 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);

    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;


    int scaleFactor = Math.min(photoW / targetSize.x, photoH / targetSize.y);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);

    return bitmap;


private File createImageFile() throws IOException 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);

    currentPhotoPath = image.getAbsolutePath();

    return image;


清单:

  <?xml version="1.0" encoding="utf-8"?>

<uses-feature android:name="android.hardware.camera"
    android:required="true"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_icon_hdx"
    android:label="@string/app_name"
    android:roundIcon="@drawable/ic_icon_hdx"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
    <activity android:name=".MainActivity">


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Finanzen" />
    <activity android:name=".Einkaufsliste" />
    <activity android:name=".Einkaufsliste2" />
    <activity android:name=".toDoList" />
    <activity android:name=".Settings" />
    <activity android:name=".PictureActivity" />
    <activity
        android:name=".Putzplan"
        android:windowSoftInputMode="adjustResize" />
    <activity android:name=".Kalender" />
    <provider
        android:name=".GenericFileProvider"
        android:authorities="$applicationId.ur.mi.android.wgplus05.genericfileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>
</manifest>

那是路径

  <?xml version="1.0" encoding="utf-8"?>
  <paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

【问题讨论】:

我认为它的权限问题使用运行时权限 【参考方案1】:
android:authorities="$applicationId.ur.mi.android.wgplus05.genericfileprovider"

BuildConfig.APPLICATION_ID + ".genericfileprovider"

这些不匹配。他们需要匹配。否则,FileProvider 找不到ContentProvider,因为您向getUriForFile() 提供了无效的权限名称。

因此,例如,您可以将第一行更改为:

android:authorities="$applicationId.genericfileprovider"

然后,这两个值将匹配(应用程序 ID 加上 .genericfileprovider)。

【讨论】:

好吧,如果我这样做:android:authorities="$applicationId.genericfileprovider" 它应该可以工作吗? @boom3x:至少它应该能让你克服这个错误。您还有其他问题(android.hardware.camera.autofocus 不是权限,如果配置更改或当相机应用程序处于前台时您的进程被终止,您不会保留currentPhotoPath)。 This sample app 演示如何将ACTION_IMAGE_CAPTUREFileProvider 一起使用。【参考方案2】:

getUriForFile 中的第二个参数应与 xml 中的 authorities 属性匹配。

BuildConfig.APPLICATION_ID + ".ur.mi.android.wgplus05.genericfileprovider"

【讨论】:

以上是关于API上的XML相机意图上的空对象=> 24 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

在两个活动之间使用相机图像处理意图并将其传递给 Google Maps API 上的标记不起作用

为啥使用相机意图捕获的图像会在 Android 上的某些设备上旋转?

如何使用 restkit 忽略已发布对象上的空属性

Android 相机意图 FileUriExposedException for SDK >= 24

相机意图不保存照片

setAdapter 上的空对象引用错误