相关疑难解决方法(0)

android减少摄像头捕获图像的文件大小小于500 kb

我的要求是将摄像头捕获的图像上传到服务器,但它应该小于500 KB.如果它大于500 KB,则需要将其减小到小于500 KB的大小(但稍微接近它)

为此,我使用以下代码 -

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == getActivity().RESULT_OK) {

                    if (requestCode == REQUEST_CODE_CAMERA) {

                        try {

                            photo = MediaStore.Images.Media.getBitmap(
                                    ctx.getContentResolver(), capturedImageUri);
                            String selectedImagePath = getRealPathFromURI(capturedImageUri);

                            img_file = new File(selectedImagePath);

                            Log.d("img_file_size", "file size in KBs (initially): " + (img_file.length()/1000));

                            if(CommonUtilities.isImageFileSizeGreaterThan500KB(img_file)) {
                                photo = CommonUtilities.getResizedBitmapLessThan500KB(photo, 500);
                            }
                            photo = CommonUtilities.getCorrectBitmap(photo, selectedImagePath);


//  // CALL THIS METHOD TO GET THE URI FROM THE BITMAP

                            img_file …
Run Code Online (Sandbox Code Playgroud)

android image filesize android-camera-intent onactivityresult

21
推荐指数
1
解决办法
9897
查看次数