即时应用程式摄影意图 [英] Instant Apps Camera Intent

查看:75
本文介绍了即时应用程式摄影意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了Instant App,我想用相机拍照. 如果启动已安装的应用程序,一切正常.但是使用即时应用,会出现以下错误:

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 }

这是我的代码:

AndroidManifest.xml

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

活动:

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

我在装有Android 7.0的设备(三星)上进行开发. 我检查了可用权限,然后使用Camera,这就是它应该起作用的原因. ( https://developer.android.com/topic/instant- apps/faqs.html#available-permissions ) 预先感谢.

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.

推荐答案

不幸的是,我认为无法通过MediaStore.ACTION_IMAGE_CAPTURE意图捕获照片.即使活动可以开始,它也需要对外部存储的写访问权才能真正发回完整的图像,并且外部存储对Instant Apps不可用(请参阅 FileProvider 目前,如果捕获意图可以写入内部存储(我不确定).

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).

虽然支持权限android.permission.CAMERA,但是您只需要使用此处尝试一个代码示例.

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.

这篇关于即时应用程式摄影意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆