简体   繁体   English

Android意向-将uri传递给相机

[英]Android intent - pass uri to camera

I am trying to test the sample code related to capturing an image via camera. 我正在尝试测试与通过摄像头捕获图像有关的示例代码 The documentation says that a URI can be passed as an extra in the intent were the camera will save the image. 该文档说,可以将URI作为额外的目的传递,因为相机将保存图像。

I tried the following : 我尝试了以下方法:

// image1 doesn't exist
File file = new File(getFilesDir() + "/image1");
Uri uri = Uri.fromFile(file);

Intent i = new Intent();
i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);

if (i.resolveActivity(getPackageManager()) != null) {
     startActivityForResult(i, 1337);
}

I am trying to place the image as a file named image1 in my files directory. 我试图将图像作为名为image1的文件放置在我的文件目录中。 I am testing this on the genymotion VM. 我正在genymotion VM上对此进行测试。 I have tested getting the image as a bitmap in the return Intent, but when I use the above approach, the camera app gets stuck when I click done after taking the picture. 我已经测试了在返回Intent中将图像作为位图进行获取,但是当我使用上述方法时,在拍照后单击“完成”时,相机应用卡住了。

I'm guessing it has something to do with URI permissions. 我猜想它与URI权限有关。 Do I need to add some permissions in the intent like in data sharing ? 我是否需要在意图中添加一些权限,例如在数据共享中?

Edit : 编辑

I tried to follow these instructions , except I want to save the photo in my app's directory, so I tried the following but it doesn't work (the app has camera permission) : 我尝试按照这些说明进行操作 ,但我想将照片保存在应用程序的目录中,因此我尝试了以下操作,但它不起作用(该应用程序具有摄像头许可):

 String imagePath = null;
 try {
      File image = File.createTempFile("testImage", ".jpg", getFilesDir());
      imagePath = "file://" + image.getAbsolutePath();
 }
 catch(Exception e){
    Toast.makeText(getApplicationContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
 }

 File file = new File(imagePath);
 Uri uri = Uri.fromFile(file);


 Intent i = new Intent();
 i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
 i.putExtra(MediaStore.EXTRA_OUTPUT, uri);

 if (i.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(i, 1337);
 }

I also have onActivityResult(), but its no use, as the camera app gets stuck as explained above. 我也有onActivityResult(),但是它没有用,因为相机应用程序如上所述卡住了。

Also, an additional question : When I don't have camera permission in my test app, I can still invoke the camera and the get the bitmap in intent extra, how so ? 另外,还有一个问题 :当我的测试应用程序中没有相机许可时,我仍然可以调用相机并获取意向额外的位图,怎么办? Is this something specific to genymotion VM ? 这是genymotion VM特有的吗?

Make sure you have these permissions for your manifest 确保您对清单具有这些权限

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

Everything looks good to me. 一切对我来说都很好。 Do you have onActivityResult() 你有onActivityResult()

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM