【问题标题】:Cannot preview captured high quality image无法预览捕获的高质量图像
【发布时间】:2019-02-12 14:03:36
【问题描述】:

我正在关注android developers guide 以捕获高质量图像并将其保存到存储中然后显示。问题是在捕获图像后我被重定向到主要活动而不显示预览,尽管图像已在存储中成功创建

public void take_hq_image(View view) {
    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) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, HIGH_Q_IMAGE_CAPTURE_REQUEST);
        }
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == HIGH_Q_IMAGE_CAPTURE_REQUEST && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap bitmap = (Bitmap) extras.get("data");
        HQimageView.setImageBitmap(bitmap);
    }
}

【问题讨论】:

  • 您正在使用EXTRA_OUTPUT。结果,返回的Intent 中没有额外的"data"。你的图片应该在photoFile——从那里加载它。见this sample app
  • 是HQimageView.setImageBitmap(bitmap);相机完成时调用? HQimageView 初始化了吗?

标签: android android-camera image-capture


【解决方案1】:

将图像路径保存为 currentImagePath 变量中的字符串

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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

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

然后显示预览

Bitmap bitmap = BitmapFactory.decodeFile(currentImagePath);
HQimageView.setImageBitmap(bitmap);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多