1

在我的应用中,用户要么从他们的图片库中挑选一张图片,要么用他们的相机拍摄一张图片。

当您捕获它时,它会添加到用户的图库中。

我发现这些图像,除了是相同的图像之外,尺寸也有很大的变化。

捕获:33kb

代码:

    if (requestCode == TAKEIMAGE) {
        System.out.println("TAKE IMAGE");
        int photoLength = 0;
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        photoLength = GetBitmapLength(photo);

        SetSubmissionImage(photo, photoLength);
    }  

取自画廊:1600kb

代码:

    else if (requestCode == CHOOSEIMAGE) {
        System.out.println("CHOOSE IMAGE");

        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        try {
            FileInputStream fileis=new FileInputStream(selectedImagePath);
            BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
            byte[] bMapArray= new byte[bufferedstream.available()];
            bufferedstream.read(bMapArray);

            int photoLength = 0;
            Bitmap photo = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);

            bMapArray = null;
            fileis.close();
            bufferedstream.close();



            //rotate to be portrait properly
            try {
                ExifInterface exif = new ExifInterface(selectedImagePath);
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                Log.e("EXIF", "Exif: " + orientation);
                Matrix matrix = new Matrix();
                if (orientation == 6) {
                    matrix.postRotate(90);
                }
                else if (orientation == 3) {
                    matrix.postRotate(180);
                }
                else if (orientation == 8) {
                    matrix.postRotate(270);
                }
                photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true); // rotating bitmap
            }
            catch (Exception e) {

            }

            //photo = GetCompressedBitmap(photo);
            photoLength = GetBitmapLength(photo);
            SetSubmissionImage(photo, photoLength);

        } 
4

1 回答 1

1

就像其他评论中所说的那样,您需要使用包含将保存图像的 uri 的意图来启动相机活动:

// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

然后在您的 onActivityResult() 方法中,您可以检索 uri 并从意图中的 URI 加载图像。

于 2013-04-03T08:42:15.220 回答