M
M
Max_Tr2019-05-13 00:43:37
Java
Max_Tr, 2019-05-13 00:43:37

How to fix photo decoding?

Hello.
Can anyone suggest what is the problem?
I receive an array of bytes from the server and then convert it to an imageView. The problem is that when I used the tomcat maven plugin everything was fine, but as soon as I started using just the tomcat 8 version, it turned out that
the method
BitmapFactory.decodeByteArray
started returning null .
Error (or rather null) started showing after using TomCat 8 and 9 (tried 7 as well)
All code

if (message.getImageMessage() != null) {
            MessageViewHolder2 holder = new MessageViewHolder2();
            LayoutInflater messageInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            byte [] ar = message.getImageMessage().getByteArray();
            Bitmap bmp = BitmapFactory.decodeByteArray(ar , 0, ar.length);

            if (bmp!=null) {

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.JPEG, 10, out);
                byte[] byteArray = out.toByteArray();


                // MY IMAGE
                //   if (message.getImageMessage().isFromMe()) {
                // Convert bytes data into a Bitmap
                convertView = messageInflater.inflate(R.layout.image_my_message_layout, null);
                holder.messageBody = (ImageView) convertView.findViewById(R.id.imageMessageView);
                holder.messageBody.setImageBitmap(bmp);


                //Вычисляем ширину и высоту изображения
                double width = bmp.getWidth();
                double height = bmp.getHeight();

                double koef = height / 1000;
                int newWidht = (int) (width / koef);
                int newheight = (int) (height / koef);

                Bitmap bmHalf = Bitmap.createScaledBitmap(bmp, newWidht, newheight, false);
                holder.messageBody.setImageBitmap(bmHalf);
            }
        }

Code for getting an image on the server
public  int size =1_000_000;
int sizeFoto = 0;
public  ByteBuffer bbuf = ByteBuffer.allocate(size);

@OnMessage
public void processUpload(ByteBuffer msg, boolean last, Session session) {

    sizeFoto +=msg.array().length;
    if(msg.remaining()!=1) {
        bbuf.put(msg);
    }
    else{
        System.err.println("Size : "+sizeFoto);
        bbuf.put(msg);
        sendBuf(bbuf);
    }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question