使用Android中的位图压缩图像时出现错误 [英] I'm getting error when I'm compress the image using Bitmap in android

查看:150
本文介绍了使用Android中的位图压缩图像时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用意图捕获图像并将其发送到我的服务器,但是当我捕获图像并进行压缩时,则某些移动设备会出现bitmap.compress错误. 那么如何解决这个问题 这是我的代码

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. 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());
        }

错误是

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: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:位于android.app.Activity.dispatchActivityResult(Activity.java:6919) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err:位于android.app.ActivityThread.deliverResults(ActivityThread.java:4174) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err:位于android.app.ActivityThread.handleSendResult(ActivityThread.java:4221) 12-30 15:33:23.488 9564-9564/com.riya.product.intranet W/System.err:位于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:位于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(本机方法) 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.internal.os.ZygoteInit.main(ZygoteInit.java:936)

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.java: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.internal.os.ZygoteInit.main(ZygoteInit.java:936)

推荐答案

替换

OutputStream outputStream = null; 

OutputStream outputStream = new FileOutputStream(f); 

这篇关于使用Android中的位图压缩图像时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆