我的要求是将摄像头捕获的图像上传到服务器,但它应该小于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