Thursday, September 29, 2011

send json object to a json request

Through Below code we can add the json object to json request in android

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(HTTPname);
        JSONObject json=new JSONObject();

        JSONArray Text_Field_Array = new JSONArray();

        JSONObject json_Text_Field = new JSONObject();
        json_Text_Field.put("pagenum", "1");
        json_Text_Field.put("text", "text field 1");
        json_Text_Field.put("font", "Arial");
        json_Text_Field.put("fontSize", "18");
        json_Text_Field.put("color", "ff0000");
        json_Text_Field.put("x", "100");
        json_Text_Field.put("y", "0");
        json_Text_Field.put("alignment", "left");

        Text_Field_Array.put(json_Text_Field);


        JSONArray image_Field_Array = new JSONArray();
        JSONObject json_image_Field = new JSONObject();
       
       
        File f=new File("/sdcard/SelectImage.png");
        ContentBody cb=new FileBody(f,"image/png");
//        reqEntity.addPart("point_image", cb);
       
        json_image_Field.put("image", cb);
        json_image_Field.put("pagenum", "1");
        json_image_Field.put("scale", "1");
        json_image_Field.put("rotation", "90");
        json_image_Field.put("x", "100");
        json_image_Field.put("y", "10");
        json_image_Field.put("width", "100");
        json_image_Field.put("height", "100");

        image_Field_Array.put(json_image_Field);

        json.put("images", image_Field_Array);
        json.put("all_text_fields", Text_Field_Array);
        json.put("sku", "CCI1022537");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("carddata", json.toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = httpclient.execute(httppost);
        InputStream is = response.getEntity().getContent();

        if(response!=null){ 
            sb = new StringBuilder();
            byte[] buffer = new byte[512];
            int bytes_read = 0;

            while((bytes_read = is.read(buffer, 0, buffer.length)) > 0)
            {
                sb.append( new String(buffer,0,bytes_read));
            }
            System.out.println("Preview image Result=="+sb.toString());
        }

Friday, September 16, 2011

store bitmap into sdcard and get it again

/////////////////////////////////////////////////////////////////////////////////

        /////          below code is used to save the bitmap into sdcard   ////////////////////

        /////////////////////////////////////////////////////////////////////////////////

        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();;
        OutputStream outStream = null;
        File file = new File(extStorageDirectory, "SlectImage.PNG");
        try {
            outStream = new FileOutputStream(file);
            picturebitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();

            Toast.makeText(PhotoManipulation.this, "Saved", Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(PhotoManipulation.this, e.toString(), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(PhotoManipulation.this, e.toString(), Toast.LENGTH_LONG).show();
        }


        /////////////////////////////////////////////////////////////////////////////////

        /////          below code is used to get the saved image and set to imageview    ////////////////////

        /////////////////////////////////////////////////////////////////////////////////

        final String imageInSD = "/sdcard/SlectImage.PNG";

        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);

        ImageView myImageView = (ImageView)findViewById(R.id.imageview);
        myImageView.setImageBitmap(bitmap);

Scroll the image both horizontal and vertically


main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <FrameLayout android:layout_width="90px"
        android:layout_height="90px">
        <RelativeLayout android:id="@+id/container"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
        </RelativeLayout>
    </FrameLayout>
</FrameLayout>


Test.Java

public class Test extends Activity
{
    private RelativeLayout container;
      private int currentX;
      private int currentY;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        container = (RelativeLayout)findViewById(R.id.container);

        int top = 0;
        int left = 0;

        ImageView image1 =new ImageView(this);
        image1.setImageResource(R.drawable.icon);//set the image whatever u scroll
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);              
        container.addView(image1, layoutParams);

       
      }    

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                currentX = (int) event.getRawX();
                currentY = (int) event.getRawY();
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                int x2 = (int) event.getRawX();
                int y2 = (int) event.getRawY();
                container.scrollBy(currentX - x2 , currentY - y2);
                currentX = x2;
                currentY = y2;
                break;
            }  
            case MotionEvent.ACTION_UP: {
                break;
            }
        }
          return true;
      }
    }

Check the internet service

Below code is used to check the INTERNET service

 ConnectivityManager connectivityManager;
        NetworkInfo wifiInfo, mobileInfo;
   
      
        public Boolean isNetAvailable(Context con)
        {
   
            try{
                connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
                wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
   
                if(wifiInfo.isConnected() || mobileInfo.isConnected())
                {
                    return true;
                }
            }
            catch(Exception e){
               e.printStackTrace();
            }
   
            return false;
        }



and following permissions are required for this


<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>


Send an Url link to mail through aplication

String mailBody="Hello. I think you’d really enjoy this:"+
                "<a href='www.google.com'> Serach Engine   </a>"+//Serach Engine  +"<br/>"+

                "<br/> Name : <br/>"+
                "<br/> Ingredients"+"<br/>"+"<br/>";


                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("text/html");
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Serach Engine");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,Html.fromHtml(mailBody));                       
                startActivity(Intent.createChooser(emailIntent, "Send mail..."));   

Json Parsing

By using JSON jar file i am easily parse the JSON String.we get that jar file from below link    JSON jar file