Search This Blog

Showing posts with label Bitmap. Show all posts
Showing posts with label Bitmap. Show all posts

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(R.id.btn_browse);
        btnBrowse.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(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(R.id.thumb_url);
           imageBrowse.setText(selectedImagePath);
           byte[] strBytes = convertToBytes(selectedImagePath);
           
           imageBytes = strBytes;
       }
   }
}
    


    public byte[] convertToBytes(String selectedImagePath)
    {
    try
    {
    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)
    {
    ex.printStackTrace();
    return null;
   
    catch (FileNotFoundException e)
    {  
e.printStackTrace();
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
           .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
   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.