【问题标题】:Photo via intent does not work通过意图的照片不起作用
【发布时间】:2015-10-15 17:42:57
【问题描述】:

我使用来自开发者网站https://developer.android.com/training/camera/photobasics.html的代码

btnPhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Log.d("Button", "take photo");
                dispatchTakePictureIntent();
            }
        });
    }
    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        Log.d("storageDir",storageDir.toString());
        //File storageDir = "/Android/data/" + getApplicationContext().getPackageName() + "/files/";
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

如果按下按钮,意图开始,我可以拍照,相机关闭但不拍照(任何文件夹中都没有照片)。我不知道为什么,代码应该可以工作。

【问题讨论】:

    标签: android


    【解决方案1】:

    您是如何得出“图片”文件夹中没有文件的结论的?当您在 Android 中创建任何新文件时,它不会立即反映。要将其反映在图库中,您需要通过 MediaScannerConnection 对其进行扫描。

    MediaScannerConnection.scanFile(getActivity(), new String[]{image.toString()}, null, null);
    

    但如果不是这样:在你的 onActivityResult() 中,查询你收到的意图对象,看看你是否有你捕获的图像的位图。

    我希望这会有所帮助:capturing images with MediaStore.ACTION_IMAGE_CAPTURE intent in android

    【讨论】:

    • 因为当我使用任何文件管理器浏览到文件夹并且没有文件时,我认为没有文件。它与媒体扫描仪无关
    • @Piet 文件管理器,或者如果您连接到 PC,在扫描之前不会显示新创建的文件。另外,尝试从代码本身取回文件,看看文件是否存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    相关资源
    最近更新 更多