简体   繁体   English

当我在 android 中使用 Bitmap 压缩图像时出现错误

[英]I'm getting error when I'm compress the image using Bitmap in android

I'm capturing image using intent and send to my server but when i'm capturing image and compress then some mobile I'm getting bitmap.compress error.我正在使用 Intent 捕获图像并将其发送到我的服务器,但是当我捕获图像并压缩时,一些移动设备出现了 bitmap.compress 错误。 So how to solve this problem guys this is my Code那么如何解决这个问题伙计们这是我的代码

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);


        File f = new File(fileUri.getPath());

        OutputStream outputStream = null;
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream = new FileOutputStream(f);
        outputStream.flush();
        outputStream.close();

        ImageView image = new ImageView(I_kycActivity.this);
        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(250, 250));
        image.setMaxHeight(400);
        image.setMaxWidth(400);
        image.setPadding(5, 5, 5, 5);
        image.setImageBitmap(bitmap);


        if (phototype.equals("Photo")) {
            img_photo.setImageBitmap(bitmap);
            txtphote.setText(f.getName());
            imgUrl.add(f.getPath());
        }

and error is错误是

12-30 15:33:23.485 9564-9564/com.riya.product.intranet W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference 12-30 15:33:23.487 9564-9564/com.riya.product.intranet W/System.err: at com.riya.product.salestracker.I_kycActivity.previewCapturedImage(I_kycActivity.java:1124) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at com.riya.product.salestracker.I_kycActivity.onActivityResult(I_kycActivity.java:1257) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:6919) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4174) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.ja 12-30 15:33:23.485 9564-9564/com.riya.product.intranet W/System.err: java.lang.NullPointerException: 尝试调用虚拟方法 'boolean android.graphics.Bitmap.compress(android.graphics. Bitmap$CompressFormat, int, java.io.OutputStream)' 在空对象引用 12-30 15:33:23.487 9564-9564/com.riya.product.intranet W/System.err: 在 com.riya.product。 salestracker.I_kycActivity.previewCapturedImage(I_kycActivity.java:1124) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err:在 com.riya.product.salestracker.I_kycActivity.onActivity I_kycActivity.java:1257) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:6919) 12-30 15 :33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4174) 12-30 15:33:23.488 9564-9564/com。 riya.product.intranet W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.ja va:4221) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread.-wrap20(ActivityThread.java) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.os.Looper.loop(Looper.java:203) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6251) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at java.lang.reflect.Method.invoke(Native Method) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at com.android va:4221) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread.-wrap20(ActivityThread.java) 12-30 15:33: 23.488 9564-9564/com.riya.product.intranet W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583) 12-30 15:33:23.488 9564-9564/com.riya .product.intranet W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err:在 android.os.Looper.loop(Looper.java:203) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: 在 android.app.ActivityThread.main(ActivityThread) .java:6251) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err: at java.lang.reflect.Method.invoke(Native Method) 12-30 15:33 :23.488 9564-9564/com.riya.product.intranet W/System.err: 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) 12-30 15:33:23.488-956 9564/com.riya.product.intranet W/System.err: 在 com.android .internal.os.ZygoteInit.main(ZygoteInit.java:936) .internal.os.ZygoteInit.main(ZygoteInit.java:936)

Replace代替

OutputStream outputStream = null; 

with

OutputStream outputStream = new FileOutputStream(f); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM