Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Problem in sending jpg and png image to server

Hi

I am facing a problem while sending image to server.

Actually i have two buttons

  1)capture image through camera

   2)select image form gallery

 

if i capture image through camera its a jpeg image and if i select it from gallery its a png image.

 

This is the code to send the jpeg image.

 

try
                    {
              
                        JPEGEncodedImage jpegEncodedImage = JPEGEncodedImage.encode(Profileimage, 10);
                        
                        //JPEGEncodedImage jpegEncodedImage = JPEGEncodedImage.encode(Profileimage, 40);
                        String boundary = "V2ymHFg03ehbqgZCaKO6jy";
                        String body1 = "--"+boundary+"\r\n"+"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""+"abc.png" +"\""+"\r\n"+"Content-Transfer-Encoding: binary"+"\r\n"+"Content-Type: image/png"+"\r\n\r\n";
                        String body2 = "\r\n"+"--"+boundary+"--\r\n";
                        
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();    
                        baos.write(body1.getBytes(), 0, body1.getBytes().length);
                        baos.write(jpegEncodedImage.getData(),0,jpegEncodedImage.getData().length);
                        baos.write(body2.getBytes(), 0, body2.getBytes().length);    

                        HttpConnection conn = (HttpConnection) Connector.open(profileUpdateUrl+ARScreen.getConnectionString(),Connector.READ_WRITE);
                        conn.setRequestMethod(HttpConnection.POST);
                        conn.setRequestProperty("Content-Type", "multipart/form-data"+";boundary="+boundary);
                        conn.setRequestProperty("Content-Length", ""+baos.toByteArray().length);
                        


                         
                            System.out.println("Response Ok..............................");
                            System.out.println("Response Ok..............................");
                            System.out.println("Response Ok..............................");
                            System.out.println("Response Ok..............................");
                            System.out.println("Response Ok..............................");
                            System.out.println("Response Ok..............................");  
                            
                        OutputStream os = conn.openOutputStream();
                        os.write(baos.toByteArray(), 0, baos.toByteArray().length);
                        os.flush();
                        os.close();
                        baos.flush();
                        baos.close();

Developer
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10
My Carrier: Vodafone

Re: Problem in sending jpg and png image to server

New Developer
Posts: 134
Registered: ‎01-11-2011
My Device: curve

Re: Problem in sending jpg and png image to server

What error your are facing.

 

 String body1 = "--"+boundary+"\r\n"+"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""+"abc.png" +"\""+"\r\n"+"Content-Transfer-Encoding: binary"+"\r\n"+"Content-Type: image/png"+"\r\n\r\n";

 

in this line you are naming file as abc.png and file is in jpeg format.Even it can genrate error.