Tuesday, November 27, 2012

download data from url using http post

HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(mcontext.getString(R.string.find_link));
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("name", value);
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        HttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        InputStream is = null;
        try {
            is = response.getEntity().getContent();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        StringBuilder sb = new StringBuilder();
        byte[] buffer = new byte[512];
        int bytes_read = 0;

        try {
            while ((bytes_read = is.read(buffer, 0, buffer.length)) > 0) {
                sb.append(new String(buffer, 0, bytes_read));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mresult=sb.toString();

Add focus to a view

mhandler.post(new Runnable() {
            public void run() {
                    (EditText ref).requestFocus();                           
               
            }
        });

Wednesday, September 5, 2012

set the text size and font to datepicker

DatePicker picker = (DatePicker)findViewById(R.id.dp_date);
        ViewGroup childpicker

         = (ViewGroup)
        findViewById(Resources.getSystem().getIdentifier("month" /*rest is:
        day, year*/, "id", "android"));

        EditText textview = (EditText)
        picker.findViewById(Resources.getSystem().getIdentifier("timepicker_input",
        "id", "android"));
        textview.setTextSize(30);
        textview.setTextColor(Color.GREEN);

Sunday, January 29, 2012

send image from url as email attachemnt


                try {
                    File rootSdDirectory = Environment.getExternalStorageDirectory();

                    File pictureFile = new File(rootSdDirectory, "attachment.jpg");
                    if (pictureFile.exists()) {
                        pictureFile.delete();
                    }                  
                    pictureFile.createNewFile();

                    FileOutputStream fos = new FileOutputStream(pictureFile);

                    URL url = new URL(string);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setDoOutput(true);
                    connection.connect();
                    InputStream in = connection.getInputStream();

                    byte[] buffer = new byte[1024];
                    int size = 0;
                    while ((size = in.read(buffer)) > 0) {
                        fos.write(buffer, 0, size);
                    }
                    fos.close();
                   
                   
                   
                   
                   
                   
                    Intent sendIntent = new Intent(Intent.ACTION_SEND);
                    //Mime type of the attachment (or) u can use sendIntent.setType("*/*")
                    sendIntent.setType("image/jpeg");
                    //Subject for the message or Email
                    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "My Picture");
                   
                    //Full Path to the attachment
                    Uri pictureUri = Uri.fromFile(pictureFile);
                    sendIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);

                    //Use a chooser to decide whether email or mms
                    startActivity(Intent.createChooser(sendIntent, "Email:"));

                } catch (Exception e) {
                    e.printStackTrace();
                  
                }