【问题标题】:Resize captured image before save保存前调整捕获的图像大小
【发布时间】:2015-11-10 15:20:14
【问题描述】:

我正在尝试调整捕获图像的大小,使其尺寸小于 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的大小

有人可以帮助我吗?谢谢

【问题讨论】:

  • 完整而准确地解释“失败”是什么意思。例如,你在崩溃吗?如果是这样,请使用 LogCat 检查与崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
  • @CommonsWare 我很抱歉。我已经编辑了我的帖子。
  • 这是正确的实现方式吗?还是我在错误的地方实施?

标签: java android size android-camera


【解决方案1】:

查看下面的链接

How to resize Image in Android?

为了更好的用户界面,请向要求上传特定尺寸图像的用户敬酒,但他仍会上传较大的图像,请在后端或服务器中检查它并给出否定响应或提示您再次上传较小尺寸的图像。

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2010-10-12
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    相关资源
    最近更新 更多