ImageView未显示使用Android中的相机捕获的图像

时间:2019-02-15 03:29:30

标签: android android-camera android-imageview

我正在尝试选择要注册的图像。当单击ImageView时,将为用户提供拍照或从他/她的画廊中进行选择的选择。当用户选择图库选项时,它将显示选定的图像。当用户选择相机选项时,ImageView不会显示图像。以下是我的OnActivityResult

代码
protected void onActivityResult(int requestCode, int resultCode, Intent imgdata) {
        super.onActivityResult(requestCode, resultCode, imgdata);
        if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK && imgdata != null) {
            selectedimage = imgdata.getData();
            try {
                InputStream inputStream = getContentResolver().openInputStream(selectedimage);
                Bitmap yourselectedimage = BitmapFactory.decodeStream(inputStream);
                imgchild.setImageBitmap(yourselectedimage);

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
            }
        }
        if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK && imgdata != null) {
            camImg =(Bitmap) imgdata.getExtras().get("img");
            try {
                imgchild.setImageBitmap(camImg);

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
            }
        }
    }

ImageView应该显示从相机拍摄的照片。

4 个答案:

答案 0 :(得分:1)

我希望这对您有用。

像这样设置图片

    Bitmap photo = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(photo);


   // CALL THIS METHOD TO GET THE ACTUAL PATH
   Uri tempUri = getImageUri(getApplicationContext(), photo);
   File finalFile = new File(getRealPathFromURI(tempUri));
   Log.e("path", finalFile.getAbsolutePath());

获取图像存储路径

 public String getRealPathFromURI(Uri uri) {
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            return cursor.getString(idx);
        }

答案 1 :(得分:0)

通过阅读代码,我认为GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); Tracker tracker = analytics.newTracker(R.xml.global_tracker); 很可能为空,因为从camImg提取位图的方式不正确。

Intent实现了Bitmap,因此您需要像这样提取它:

Parcelable

答案 2 :(得分:0)

我认为应该是这样

L2 Norm: 371.979095

Not compiled with OpenCV, saving to Original.png instead 

Not compiled with OpenCV, saving to Gray.png instead

Not compiled with OpenCV, saving to C1.png instead

Not compiled with OpenCV, saving to C2.png instead

Not compiled with OpenCV, saving to C3.png instead

Not compiled with OpenCV, saving to C4.png instead

答案 3 :(得分:0)

我遇到了与Android Marshmallow相同的问题,就我而言,这是由于位图过大导致的,所以我通过调整位图的大小来找出以下代码

'statusCode'