[android-developers] Re: Post Bitmap over Http
You might want to actually compress that JPG by passing a value less than 100. Try saving the image on your SD card, check what compression gives you a reasonable size and then set that compression and check w/ the network code. Also, you probably won't get that much benefit with buffering the output because the network is not that fast, unless you're on 3G, so either set an explicit buffer size or remove the buffering altogether. On Fri, Jan 9, 2009 at 3:18 PM, mobilek...@googlemail.com wrote: > > Hi, I added the following code, however, I got an OutOfMemoryError. It > comes from the bos.flush() line... Am I doing it wrong? I > > BufferedOutputStream bos = new BufferedOutputStream(os); > bm.compress(CompressFormat.JPEG, 100, bos); > > try { >bos.flush(); >bos.close(); >os.close(); > } catch (IOException e) { >e.printStackTrace(); > } > conn.disconnect(); > > Thank you! > > > >> >> bm.compress(CompressFormat.PNG, 100, os); >> >> Note that PNG is lossless and will ignore the 2nd argument. >> Now, the API *could* have a helper method called save(CompressFormat, >> Stream) passing the max quality as a default 2nd parameter but that's >> another story... >> >> Cheers, >> Stoyan > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Post Bitmap over Http
Hi, I added the following code, however, I got an OutOfMemoryError. It comes from the bos.flush() line... Am I doing it wrong? I BufferedOutputStream bos = new BufferedOutputStream(os); bm.compress(CompressFormat.JPEG, 100, bos); try { bos.flush(); bos.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } conn.disconnect(); Thank you! > > bm.compress(CompressFormat.PNG, 100, os); > > Note that PNG is lossless and will ignore the 2nd argument. > Now, the API *could* have a helper method called save(CompressFormat, > Stream) passing the max quality as a default 2nd parameter but that's > another story... > > Cheers, > Stoyan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Post Bitmap over Http
On Fri, Jan 9, 2009 at 11:55 AM, mobilek...@googlemail.com wrote: > > Hi, > > I need to send a bitmap to a server. I've written some code, however, > I got stuck on the part where I need to transform my bitmap into byte > []. Here is my code: > > public void updateBitmap(Bitmap bm){ >URL url = null; >HttpURLConnection conn = null; >try { >url = new URL("http://www.myurl.com?";); >} catch (MalformedURLException e) { >e.printStackTrace(); >} >try { >conn = (HttpURLConnection) url.openConnection > (); >} catch (IOException e) { >e.printStackTrace(); >} >try { >conn.setRequestMethod("POST"); >} catch (ProtocolException e) { >e.printStackTrace(); >} >conn.setDoOutput(true); >conn.setDoInput(true); >conn.addRequestProperty("user_id", user.getUsername > ()); >conn.addRequestProperty("pass", user.getPassword()); >conn.addRequestProperty("id", pinId); > > OutputStream os = null; >try { >os = conn.getOutputStream(); >} catch (IOException e) { >e.printStackTrace(); >} > >// Encode bitmap and flush the os bm.compress(CompressFormat.PNG, 100, os); Note that PNG is lossless and will ignore the 2nd argument. Now, the API *could* have a helper method called save(CompressFormat, Stream) passing the max quality as a default 2nd parameter but that's another story... Cheers, Stoyan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---