Android从相机获取全尺寸图像

 手机用户2502906277 发布于 2023-02-13 11:18

我正在开发一个Android应用程序,它将图像从相机或设备照片库上传到远程站点.后者我工作正常,我可以选择并上传.但是,我无法拍摄完整尺寸的图片并上传.这是我的代码:

// From onCreate
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

我有一个方法来处理Activity结果.这两个都处理从图库和相机中选择:

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

  String filepath = ""; 
  Uri selectedImageUri;

  if (resultCode == RESULT_OK) {
    if (requestCode == CAMERA_PIC_REQUEST) {
      Bitmap photo = (Bitmap) data.getExtras().get("data");
      // Gets real path of image so it can be uploaded
      selectedImageUri = getImageUri(getApplicationContext(), photo);
    }
    else {
      selectedImageUri = data.getData();
    }
    // Handle the upload ...
  }
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

但是,这有效,图像很小,不符合存储图像的命名约定.我已经读过要保存完整大小我需要使用putExtra并在cameraIntent上传递MediaStore.EXTRA_OUTPUT并声明一个临时文件,所以我调整了我的意图代码如下:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
camImgFilename = sdf.format(new Date())+".jpg";

File photo = new File Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), camImgFilename);

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

然而,这会导致抛出错误,指出"未找到来源".

不知道从哪里开始?

UPDATE

看来我的照片正在创作中.应用程序关闭后,我可以使用文件资源管理器导航到目录并查看图像.我不确定导致应用程序崩溃的原因.

1 个回答
  • 我认为问题是你仍然试图从中访问缩略图Intent.要检索完整大小的图像,您需要直接访问该文件.所以我建议在开始摄像机活动之前保存文件名,然后再加载文件onActivityResult.

    我建议您阅读官方文档中的" 拍摄照片"页面,您可以在其中找到有关缩略图的引用:

    注意:来自"数据"的缩略图图像可能适用于图标,但不是更多.处理完整尺寸的图像需要更多的工作.

    同样在最后一节中,您将找到所需的代码:

    private void setPic() {
        // Get the dimensions of the View
        int targetW = mImageView.getWidth();
        int targetH = mImageView.getHeight();
    
        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;
    
        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
    
        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;
    
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        mImageView.setImageBitmap(bitmap);
    }
    

    2023-02-13 11:19 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有