Search This Blog

Tuesday, February 28, 2012

Uploading Image to the server using webservice

first of all reach to the gallery of the phone using the intent.

Button btnBrowse = (Button)findViewById(;
        btnBrowse.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Intent intent = new Intent();
                "Select Picture"), SELECT_PICTURE);


Now onActivityResult should be like this.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == RESULT_OK) {
       if (requestCode == SELECT_PICTURE) {
           Uri selectedImageUri = data.getData();
           selectedImagePath = getPath(selectedImageUri);
           EditText imageBrowse = (EditText)findViewById(;
           byte[] strBytes = convertToBytes(selectedImagePath);
           imageBytes = strBytes;

    public byte[] convertToBytes(String selectedImagePath)
    FileInputStream fs = new FileInputStream(selectedImagePath);

    Bitmap bitmap = BitmapFactory.decodeStream(fs);
    ByteArrayOutputStream bOutput = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG,1, bOutput);
    byte[] dataImage = bOutput.toByteArray();
    return dataImage;
    catch(NullPointerException ex)
    return null;
    catch (FileNotFoundException e)
return null;

public String getPath(Uri uri) {
   String[] projection = { MediaStore.Images.Media.DATA };
   Cursor cursor = managedQuery(uri, projection, null, null, null);
   int column_index = cursor
   return cursor.getString(column_index);

Using this bytes you'll get the image uploaded to the web service and store the image on to the server.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.