【问题标题】:Android intent - pass uri to cameraAndroid意图 - 将uri传递给相机
【发布时间】:2014-06-18 23:29:22
【问题描述】:

我正在尝试测试与通过相机捕获图像相关的sample code。文档说,如果相机将保存图像,则可以在意图中作为额外的 URI 传递。

我尝试了以下方法:

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

我试图将图像作为一个名为 image1 的文件放在我的文件目录中。我正在 genymotion VM 上对此进行测试。我已经测试了在返回 Intent 中将图像作为位图获取,但是当我使用上述方法时,当我在拍照后单击完成时,相机应用程序会卡住。

我猜它与 URI 权限有关。我是否需要在意图中添加一些权限,例如在数据共享中?

编辑

我尝试关注这些instructions,除了我想将照片保存在我的应用程序目录中,所以我尝试了以下但它不起作用(应用程序具有相机权限):

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

我也有 onActivityResult(),但它没有用,因为相机应用程序卡住了,如上所述。

另外,还有一个问题:当我在我的测试应用中没有摄像头权限时,我仍然可以调用摄像头并额外获取位图,这是怎么回事?这是 genymotion VM 特有的吗?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    确保您对清单具有这些权限

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

    在我看来一切都很好。你有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
    

    【讨论】:

      猜你喜欢
      • 2011-12-22
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多