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
>
>       }
>
> Could you advice me on how to complete my code? Thank you.
>   
I'm going to be tackling the same sort of problem in an app I'm working 
on so I can't give you much advice. BUT, I can tell you that typically 
with HTTP file transfers, you'll want to encode the file into base-64 
format. Unfortunately, for whatever reason, Android does not include 
that capability as far as I know. Oddly, there's a bit of code in the 
Apache Commons/Jakarta project to do that, but it wasn't included in 
Android with the other code borrowed from that project. So what I did is 
download the following files from the Apache site (I grabbed them 
directly out of the source repository browser at 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/
 
) and create new packages within my app's project:

BinaryDecoder.java
BinaryEncoder.java
Decoder.java
DecoderException.java
Encoder.java
EncoderException.java
StringDecoder.java
StringEncoder.java
StringEncoderComparator.java
binary/Base64.java
binary/Base64InputStream.java
binary/Base64OutputStream.java
binary/BinaryCodec.java
binary/Hex.java

I needed to create two packages: org.apache.commons.codec and 
org.apache.commons.codec.binary from those files; it should be easy 
enough to guess which ones go where. :-) From that point, base-64 
encoding or decoding is as simple as including the packages, and 
calling  Base64.encodeString() or Base64.decodeString(). There are 
multiple encoding/decoding functions there to suit your needs.

Raymond

P.S. I'm hoping that Google or some coder will include these classes in 
the Android sources in the future, which would make us using it like 
this unnecessary. Time will tell...

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to