2017-10-14 45 views
0

我有一個應用程序可以讓您製作一個列表,並且可以爲每個項目添加名稱,價格,說明,圖像,等等。在API 19崩潰時使用相機拍攝圖像,但它適用於API 21和更新版本

我開始用最低的API等級21編寫應用程序。我目前正在嘗試將其降低到API 19,以便我可以獲得一些KitKat用戶。

當API 19上的用戶嘗試使用該應用拍攝圖片時,會發生該問題。當他們按下「拍攝照片」按鈕時,照相機拉起來很好,但是當他們拍攝照片並點擊複選標記以保存圖像時,照相機應用程序會崩潰。

以下是相機應用程序日誌中的錯誤代碼。我看到的主線是這樣的:

Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{a517eda0 12685:com.android.camera/u0a32} (pid=12685, uid=10032) that is not exported from uid 10057 

Process: com.android.camera, PID: 12685 
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{a517eda0 12685:com.android.camera/u0a32} (pid=12685, 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)  

而且也是,因爲我相信這是一個權限的事情,這是我的AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="jeremy.com.wineofmine"> 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<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"> 
    <activity 
     android:name="jeremy.com.wineofmine.MainActivity" 

     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="jeremy.com.wineofmine.AddActivity" 
     android:label="" 
     android:parentActivityName="jeremy.com.wineofmine.MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name="jeremy.com.wineofmine.DetailsActivity" 
     android:label="" 
     android:parentActivityName="jeremy.com.wineofmine.MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name="jeremy.com.wineofmine.EditActivity" 
     android:label="" 
     android:parentActivityName="jeremy.com.wineofmine.MainActivity" 
     android:windowSoftInputMode="stateHidden" /> 

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

    <activity android:name="jeremy.com.wineofmine.ImageActivity" 
     android:parentActivityName="jeremy.com.wineofmine.DetailsActivity"></activity> 
</application> 

有沒有人知道這是怎麼回事?我很茫然。我相信我讓應用程序有權使用清單中的這兩個權限行來訪問這些文件。

如果有任何附加代碼會有幫助,請讓我知道。


更新1:

添加攝像頭意圖代碼(我沒有足夠的FLAG_GRANT_WRITE_URI_PERMISSION前行,但我把它加到由於CommonsWare的帖子它並沒有解決問題):

private void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     takePictureIntent.addFlags(FLAG_GRANT_WRITE_URI_PERMISSION); 
     // 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) { 

      } 
      // Continue only if the File was successfully created 
      if (photoFile != null) { 
       photoURI = FileProvider.getUriForFile(this, 
         "jeremy.com.wineofmine.fileprovider", 
         photoFile); 

       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
       startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 

      } 
     } 
    } 
+0

假設您正在使用'ACTION_IMAGE_CAPTURE',將'FLAG_GRANT_READ_URI_PERMISSION'添加到'Intent'。請參閱https://github.com/commonsguy/cw-omnibus/tree/v8.7/Camera – CommonsWare

+0

@CommonsWare我將相機意圖的代碼添加到OP中。 – andrdoiddev

回答

1

對不起,我在評論中倒退了。相機應用正在寫入,因此您需要將FLAG_GRANT_WRITE_URI_PERMISSION添加到Intent

UPDATE ...並且,因爲你要支持Android 4.4,你也需要做this crap

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) { 
    i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
} 
else { 
    ClipData clip= 
    ClipData.newUri(getContentResolver(), "whatevs", outputUri); 

    i.setClipData(clip); 
    i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
} 

其中:

  • iIntent
  • outputUri是您使用的值爲EXTRA_OUTPUT
+0

我剛將它改爲'takePictureIntent.addFlags(FLAG_GRANT_WRITE_URI_PERMISSION);'但它仍然在做同樣的錯誤。 :(任何其他想法? – andrdoiddev

+0

@andrdoiddev:查看更新 - 我在您的Android 4.4目標上空白。 – CommonsWare

+0

並且該代碼在該方法中的其他代碼之前?這裏是我現在的dispatchTakePictureIntent:https://pastebin.com/6jJRxcgV我不完全相信我正確實施它。我查看了你給的鏈接,我相信我是? – andrdoiddev

相關問題