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.

2
  • You should post some code of what you are attempting to do and where you are having problems, this too vague. Commented Aug 4, 2011 at 1:45
  • @Patrick Thanks!Here is the link for my code : paste.org/36893 . I have to pass the image in " SEND IMAGE as URI " in code. Commented Aug 4, 2011 at 20:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Want to send a Image to Server by using Json. So the parameter is a string type. Code is here :paste.org/36893
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.