Get Image from camera into ImageView Android

前端 未结 1 544
孤街浪徒
孤街浪徒 2021-01-13 15:11

The following code works well to get the thumbnail of a picture taken from the camera. I want to get the full image, not just the thumbnail. I have a Samsung Galaxy Nexus

1条回答
  •  不知归路
    2021-01-13 15:39

    To get a full size picture you need to first save it to file and then extract it's bitmap from it, do this:

     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
     File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
     startActivityForResult(intent, CAPTURE_NEW_PICTURE);
    

    Then in onActivityResult:

     File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
     mImage = (ImageView) findViewById(R.id.imageView1);
     mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));
    

    When decodeSampledBitmapFromFile:

        public static Bitmap decodeSampledBitmapFromFile(String path,
            int reqWidth, int reqHeight) { // BEST QUALITY MATCH
    
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
    
        // Calculate inSampleSize
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            int inSampleSize = 1;
    
            if (height > reqHeight) {
                inSampleSize = Math.round((float)height / (float)reqHeight);
            }
    
            int expectedWidth = width / inSampleSize;
    
            if (expectedWidth > reqWidth) {
                //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
    
    
        options.inSampleSize = inSampleSize;
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
    
        return BitmapFactory.decodeFile(path, options);
      }
    

    You can play with the numbers (500 and 250 in this case) to change the quality of the bitmap for the ImageView.

    0 讨论(0)
提交回复
热议问题