【问题标题】:Why does the Camera Intent not returning the image to the ImageView?为什么 Camera Intent 不将图像返回给 ImageView?
【发布时间】:2021-11-06 20:28:30
【问题描述】:

我想要实现的是在地图上长按它会调出自定义对话框视图,其中包含 3 个按钮,一个用于照片,一个用于保存,一个用于取消。

所以在点击照片的那一刻,相机意图打开,我可以拍照。单击“确定”后,意图返回到自定义对话窗口但没有显示图像?

这是我目前正在使用的代码:

public void onMapLongClick(LatLng point) {
            LayoutInflater factory = LayoutInflater.from(MainActivity.this);
            final View deleteDialogView = factory.inflate(R.layout.custom_dialog, null);
            final AlertDialog deleteDialog = new AlertDialog.Builder(MainActivity.this).create();
            deleteDialog.setView(deleteDialogView);
            deleteDialogView.findViewById(R.id.btn_photo).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    String f = System.currentTimeMillis()+".jpg"; // Designated name
                    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), f);
                    fileUri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".fileprovider", file);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                    startActivityForResult(cameraIntent, TAKE_PICTURE);
                }
                protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
                    switch (requestCode) {
                        case TAKE_PICTURE:
                            ImageView imgView = findViewById(R.id.a);
                            imgView.setImageURI(fileUri);
                            break;

                    }
                }
            });  

【问题讨论】:

  • 您检查过您在onActivityResult所做 得到的内容吗?此外,我很少接触 Android,但知道使用 File 往往会导致问题。
  • 实际行为是什么 - onActivityResult 是否被调用,文件不存在,或者该方法根本没有被调用?

标签: java android android-camera-intent


【解决方案1】:

请注意,您将“fileUri”传递给 Intent,因此您还需要从返回的 Intent 中获取它。

尝试替换imgView.setImageURI(fileUri);

imgView.setImageURI(data.getData());

Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data"); 

这将为您提供带有图像的 URI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多