Instant Apps Camera Intent

2019-04-29 08:24发布

I developed and Instant App which one I would like to take a picture with the camera. Everything work if I launch the Installed App. But with Instant App, I get the following error :

java.lang.SecurityException: Not allowed to start activity Intent { act=android.media.action.IMAGE_CAPTURE launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 }

Here my code :

AndroidManifest.xml

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

Activity :

private static int CAMERA_REQUEST = 1234;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goodbye);

findViewById(R.id.mainButton).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          startCamera();
      }
  });
}

private void startCamera() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST);
        }
    } else {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    startCamera();
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        Bitmap bmp = (Bitmap)data.getExtras().get("data");
        ((ImageView)findViewById(R.id.mainImageView)).setImageBitmap(bmp);
    }
}

I develop on device (samsung) with Android 7.0. I checked available permission and Camera is that's why it's should work. (https://developer.android.com/topic/instant-apps/faqs.html#available-permissions) Thanks in advance.

3条回答
叛逆
2楼-- · 2019-04-29 09:06

With reference to Google issue tracker, The fix will be part of android 8.1 Oreo. Unfortunately it's not possible to fix through GMS, however we are sending out a patch for the fix to our partners, so they can adopt the fix even if they aren't building off of an 8.1 base.

If any issue persists, please report at Google issue tracker they will re-open to examine.

查看更多
疯言疯语
3楼-- · 2019-04-29 09:10

I don't think capturing photos via the MediaStore.ACTION_IMAGE_CAPTURE intent will work at the moment unfortunately. Even if the activity could start, it requires write access to external storage to actually send back the full image and external storage is not available to Instant Apps (see restrictions). FileProvider is also not support on Instant Apps at the moment in case the capture intent could write to internal storage (I'm not sure about that).

The permission android.permission.CAMERA is supported though, you will just need to use the camera2 APIs. There is a code sample you can try out here.

查看更多
手持菜刀,她持情操
4楼-- · 2019-04-29 09:24

I fear the problem does not come from the permission but from the way you are launching your activity.

Instant Apps in fact cannot launch activities with explicit intents unless that specific intent has been made available to instant apps.

EDIT: Sorry I told you before that you are trying to launch an explicit intent. Actually new Intent(MediaStore.ACTION_IMAGE_CAPTURE) is an implicit one. Hence I do not understand why you have the security exception. Are you using latest canary 4 version ?

For the difference between an explicit intent and an implicit one:

  • Explicit intent target specifically another app or component

  • Implicit intent let the system choose which app should handle the intent. i.e. Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com");

查看更多
登录 后发表回答