Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instant Apps Camera Intent

Tags:

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.

like image 376
Pouicky Avatar asked Jun 29 '17 15:06

Pouicky


People also ask

What is instant apps and do I need it?

A Google Android instant app is a small software program that enables end users to test out a portion of a native Android app without installing it on a device. Instant apps, although they run like local apps, are native containers with access to a device's hardware.

What is Camera Intent?

Take a photo with a camera appAndroid delegates actions to other applications by invoking an Intent . This process involves three pieces: the Intent itself, a call to start the external Activity , and some code to handle the image data when focus returns to your activity.

What triggers an Intent in Android?

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc.

How do I open Camera with Intent?

This is done as follows: Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.


2 Answers

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

like image 181
gbaccetta Avatar answered Sep 30 '22 14:09

gbaccetta


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.

like image 21
AdamK Avatar answered Sep 30 '22 15:09

AdamK