应用程序崩溃,当我试图通过相机拍摄全尺寸照片时api低于21

时间:2018-01-29 15:29:20

标签: java android camera

我正在从官方文档中测试Android示例。我的应用程序与api 21及更高版本一起工作正常,但总是在api低于21的情况下崩溃,例如KitKat(api 19)。我没有直接使用相机,只能通过其他应用程序(制作并发送Intent)。 我当前的appcompat lib:com.android.support:appcompat-v7:27.0.2

我如何解决这个问题?

在此找到解决方案: https://medium.com/@a1cooke/using-v4-support-library-fileprovider-and-camera-intent-a45f76879d61

https://developer.android.com/training/camera/photobasics.html#TaskPath

LogCat在这里:

    01-29 15:51:14.414 3090-3090/com.android.camera D/AndroidRuntime: Shutting down VM
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressStandard.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressSpacebar.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressDelete.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressReturn.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
01-29 15:51:14.414 1573-1632/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressInvalid.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
01-29 15:51:14.414 1573-1632/system_process W/AudioService: onLoadSoundEffects(), Error -1 while loading samples
01-29 15:51:14.414 3090-3090/com.android.camera W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d75b20)
01-29 15:51:14.414 1573-1915/system_process W/ActivityManager: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{a538ebb8 3090:com.android.camera/u0a32} (pid=3090, uid=10032) that is not exported from uid 10057
01-29 15:51:14.414 3090-3090/com.android.camera E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.android.camera, PID: 3090
                                                                  java.lang.IllegalStateException: Could not execute method of the activity
                                                                      at android.view.View$1.onClick(View.java:3823)
                                                                      at android.view.View.performClick(View.java:4438)
                                                                      at android.view.View$PerformClick.run(View.java:18422)
                                                                      at android.os.Handler.handleCallback(Handler.java:733)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:136)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                      at dalvik.system.NativeStart.main(Native Method)
                                                                   Caused by: java.lang.reflect.InvocationTargetException
                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                      at android.view.View$1.onClick(View.java:3818)
                                                                      at android.view.View.performClick(View.java:4438) 
                                                                      at android.view.View$PerformClick.run(View.java:18422) 
                                                                      at android.os.Handler.handleCallback(Handler.java:733) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                      at android.os.Looper.loop(Looper.java:136) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                      at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                      at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                      at dalvik.system.NativeStart.main(Native Method) 
                                                                   Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{a538ebb8 3090:com.android.camera/u0a32} (pid=3090, uid=10032) that is not exported from uid 10057
                                                                      at android.os.Parcel.readException(Parcel.java:1465)
                                                                      at android.os.Parcel.readException(Parcel.java:1419)
                                                                      at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2848)
                                                                      at android.app.ActivityThread.acquireProvider(ActivityThread.java:4415)
                                                                      at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2207)
                                                                      at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
                                                                      at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:906)
                                                                      at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
                                                                      at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
                                                                      at com.android.camera.Camera.doAttach(Camera.java:1385)
                                                                      at com.android.camera.Camera.onReviewDoneClicked(Camera.java:1362)
                                                                      at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                      at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                      at android.view.View$1.onClick(View.java:3818) 
                                                                      at android.view.View.performClick(View.java:4438) 
                                                                      at android.view.View$PerformClick.run(View.java:18422) 
                                                                      at android.os.Handler.handleCallback(Handler.java:733) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                      at android.os.Looper.loop(Looper.java:136) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                      at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                      at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                      at dalvik.system.NativeStart.main(Native Method) 

这里的简单代码:

<application>
   ...
   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
    ...
</application>


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>

String mCurrentPhotoPath;
static final int REQUEST_TAKE_PHOTO = 1;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        ...
    }
}

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
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}




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

private void setPic() {
    // Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView.setImageBitmap(bitmap);
}

1 个答案:

答案 0 :(得分:0)

FLAG_GRANT_READ_URI_PERMISSION添加到takePictureIntent。目前,相机应用程序无权使用Uri

有关ACTION_IMAGE_CAPTUREFileProvider一起使用的完整实施,请参见this sample app