0

I want to send a image to server in a JSON webservice (by using a string parameter in JSON post method) and i want to get image from URI path.

Subrat
  • 3,718
  • 6
  • 34
  • 48
  • You should post some code of what you are attempting to do and where you are having problems, this too vague. – Patrick Kafka Aug 04 '11 at 01:45
  • @Patrick Thanks!Here is the link for my code : http://goo.gl/Bhe24 . I have to pass the image in " SEND IMAGE as URI " in code. – Subrat Aug 04 '11 at 20:24

2 Answers2

1

The folowing class is what i use to download a picture (in my case it was a jpeg) from a given url. It is a piece from my own code so there may be some project specific stuff in there. Just read past that:).

public class BitmapFromUrl 
{
    private Bitmap myBitmap;

    public BitmapFromUrl(String imageUrl) 
    {
        URL myImageURL = null;

        try 
        {
            myImageURL = new URL(imageUrl);
        } 
        catch (MalformedURLException error) 
        {
            Log.e("tag", "The URL could not be formed from the provided String" + error);
        }

        if((myImageURL != null) && (imageUrl != null)) {
            try 
            {
                HttpURLConnection connection = (HttpURLConnection)myImageURL .openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                myBitmap = BitmapFactory.decodeStream(input);
            } 
            catch (IOException e) 
            {
                Log.e("tag", "The Bitmap could not be downloaded or decoded!" + e);
            }
        } else {
            Log.e("tag", "The provided URL(\"" + imageUrl + "\") does not seem to be valid.");
            myBitmap = null;
        }
    }

    public Bitmap getBitmap() 
    {
        return myBitmap;
    }
}

To send get a String of this image you can use the following:

Bitmap bm = BitmapFactory.decodeFile("/thePathToYour/image.jpeg");
ByteArrayOutputStream output = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, output); //bm is the bitmap object   
byte[] bytes = output.toByteArray();

String base64Image = Base64.encode(bytes, Base64.DEFAULT);

Now you have the image as a string. On the server you can change it back to an image using the Base64 methods of your servers programming language.

json.put(WebConstant.JSON_PUT_PROPERTY_BUZZ_IMAGE, base64Image);

This should do the job.

Manuel
  • 9,453
  • 5
  • 39
  • 58
  • Want to send a Image to Server by using Json. So the parameter is a string type. Code is here :http://goo.gl/Bhe24 – Subrat Aug 04 '11 at 20:26
0

in your line:

json.put(WebConstant.JSON_PUT_PROPERTY_BUZZ_IMAGE, " SEND IMAGE as URI ");//buzzInfoBean.getBuzzImage());

"SEND IMAGE as URI " should be a base64 encoded string most likely, I don't know what the server is expecting, but that is most common.

Check out the answer to this question

Getting the image from the local uri

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri1)
Community
  • 1
  • 1
Patrick Kafka
  • 9,516
  • 3
  • 27
  • 43