2016-08-08 21 views
0

URIを表示しようとすると次の例外が発生します。私はそれが私の活動が止まった後に起こり、実行可能なURIに再びアクセスしようとしていると思います。この問題に対処する他の質問がありますが、私のtakePhotoIntentは写真を撮るかギャラリーから選択することができるので、コードにどのような解決策を適用するかについては非常に混乱しています(下記参照)。アクセス許可拒否:MediaDocumentsProvider

Unable to open content: content://com.android.providers.media.documents/document/image%3A49688 
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri 
content://com.android.providers.media.documents/document/image%3A49688 from pid=821, uid=10238 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission() 

マイcreateImageFileと私のtakePhotoIntent:Storage Access Framework client documentationパー

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 = "file:" + image.getAbsolutePath(); 
    return image; 
} 

private void dispatchTakePictureIntent() { 
    for(int i = 0; i < 4; i++) { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      // Create the File where the photo should go 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
       outputFileUri = Uri.fromFile(photoFile); 
      } catch (IOException ex) { 
       Log.w("error","IOException"); 
      }catch (NullPointerException nullEx) { 
       Log.w("error","NullPointerException"); 
      } 
      // Camera. 
      final List<Intent> cameraIntents = new ArrayList<Intent>(); 
      final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      final PackageManager packageManager = getPackageManager(); 
      final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); 
      for (ResolveInfo res : listCam) { 
       final String packageName = res.activityInfo.packageName; 
       final Intent intent = new Intent(captureIntent); 
       intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
       intent.setPackage(packageName); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
       cameraIntents.add(intent); 
      } 
      // Filesystem. 
      final Intent galleryIntent = new Intent(); 
      galleryIntent.setType("image/*"); 
      galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 
      // Chooser of filesystem options. 
      final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); 
      // Add the camera options. 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()])); 
      if(id.equals(HAPPY_ID)) 
       startActivityForResult(chooserIntent, REQUEST_HAPPY_PHOTO); 
      if(id.equals(SURPRISED_ID)) 
       startActivityForResult(chooserIntent, REQUEST_SURPRISED_PHOTO); 
      if(id.equals(AFRAID_ID)) 
       startActivityForResult(chooserIntent, REQUEST_AFRAID_PHOTO); 
      if(id.equals(UPSET_ID)) 
       startActivityForResult(chooserIntent, REQUEST_UPSET_PHOTO); 
      if(id.equals(SAD_ID)) 
       startActivityForResult(chooserIntent, REQUEST_SAD_PHOTO); 
     } 
    } 
} 

答えて

1

:あなたはあなたのアプリケーションは、単に/インポートデータを読みたい場合は

  • 使用ACTION_GET_CONTENT。このアプローチでは、アプリケーションは画像ファイルなどのデータのコピーをインポートします。

  • あなたのアプリに、ドキュメントプロバイダが所有するドキュメントへの永続的な長期アクセス権を持たせたい場合は、ACTION_OPEN_DOCUMENTを使用してください。たとえば、ユーザーがドキュメントプロバイダに保存されているイメージを編集できるようにするフォト編集アプリケーションがその例です。

あなたがACTION_GET_CONTENTを使用しているなどのコンポーネントが破棄されたとき(あなたが新しいコンポーネントにURIを渡さない限り)、ウリへのアクセスが終了します。 Uriからファイルのコピーを作成し、コンポーネントの存続期間を超えてアクセスしたい場合は、ACTION_OPEN_DOCUMENTpersist permissionsに切り替えてください(文書Urisでのみ可能です)。

+0

ありがとうございます!これはドキュメントに記載されているはずです。 – yuku

+0

@yuku - 私の答えの半分が文字通りドキュメントを引用しています – ianhanniballake

+0

別のページに記載されています:https://developer.android.com/guide/topics/providers/content-provider-basics.html#Intents 私はdidn 'それを見てください。ありがとう! – yuku

関連する問題