保存前调整捕获的图像大小

时间:2015-11-10 15:20:15

标签: java android size android-camera

我正在尝试调整捕获的图像的大小,使其大小小于1Mb。这是我迄今为止在 OnActivityResult 中尝试但失败的原因。我从Android take photo and resize it before saving on sd card

获得了教程

ImageFitScreen.java

 try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmapOptions.inJustDecodeBounds = false;
                    bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
                    bitmapOptions.inDither = true;
                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                    Global.img = bitmap;

                    b.setImageBitmap(bitmap);
                    String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
                    //p = path;
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png");
                    try {

                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outFile);
                        //pic=file;
                        outFile.flush();
                        outFile.close();


                    } catch (FileNotFoundException e) {
                        e.printStackTrace();

                    } catch (IOException e) {
                        e.printStackTrace();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }

            }

Claims.java

 button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
                   // Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
                    byte[] data=getBitmapAsByteArray(getActivity(),Global.img);// this is a function
                    Toast.makeText(getActivity().getApplicationContext(), data+"", Toast.LENGTH_LONG).show();

                    if(data==null)
                    {
                        Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();
                    }
                    else
                    {


                        Toast.makeText(getActivity(), " not null", Toast.LENGTH_LONG).show();
                        SB.insertStaffBenefit(name, data, description, result, fk);
                    }

        });
        return claims;
    }

    public static byte[] getBitmapAsByteArray(final Context context,Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
        Toast.makeText(context, outputStream.size()/1024+"KB", Toast.LENGTH_LONG).show();
        return outputStream.toByteArray();
    }

在Claims.java中,它仍然显示超过1Mb的大小

有人能帮助我吗?感谢

1 个答案:

答案 0 :(得分:0)

查看以下链接

How to resize Image in Android?

为了更好的用户界面向用户提供要求上传特定尺寸图像的干杯,他上传更大的图像,在后端或服务器中检查并给出否定回复或提示您再次上传较小尺寸的图像。