Sunday, January 27, 2013

Drawing Layers and Color shapes on Bitmap


Here We can Draw the Colors and keep the multiple drawable images as Layers on Bitmab.
Thwe code is as follows.

Resources r = getResources();

        Drawable[] layers = new Drawable[3];

Drawing color on a bitmap as first layer
        layers[0] = writeOnDrawable((R.drawable.some, text);

Drawing bitmap from resources using R.drawable.some  as Second layer
layers[2] = r.getDrawable(ImgUtil.getImageId(R.drawable.some));

Drawing Border to the Bitmap  as Third layer
layers[1] = getBorder(ImgUtil.getImageId((R.drawable.some), "");


All the above layers as single image by using LayerDrawables class
        LayerDrawable layerDrawable = new LayerDrawable(layers);




public BitmapDrawable writeOnDrawable(int drawableId, String text, String combineTblId){

       Bitmap bm = BitmapFactory.decodeResource(getResources(),                     drawableId).copy(Bitmap.Config.ARGB_8888, true);
       Paint paint = new Paint();
//        paint.setStyle(Style.FILL_AND_STROKE);
       paint.setColor(Color.BLACK);
       paint.setTextSize(20);
       paint.setTypeface(Typeface.DEFAULT_BOLD);
     
       Canvas canvas = new Canvas(bm);
       canvas.drawText(text, bm.getWidth()/2-10, bm.getHeight()/2+8, paint);
       String color = combinedTableMap.get(combineTblId);
       if(color!= null){
       
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
       paint.setStrokeWidth(2);
       paint.setColor(Integer.parseInt(color));    
       paint.setStyle(Paint.Style.FILL_AND_STROKE);
       paint.setAntiAlias(true);
     
        Path path = new Path();
       path.setFillType(Path.FillType.EVEN_ODD);
       path.moveTo(0,0);
       path.lineTo(0,15);
       path.lineTo(15,0);
       path.lineTo(0,0);
       path.close();
       canvas.drawPath(path, paint);
       }
     
     
       /*paint.setStyle(Style.STROKE);
       paint.setColor(Color.RED);
       paint.setStrokeWidth(2);
       canvas.drawRect(new RectF(0,0, bm.getWidth(), bm.getHeight()), paint);*/
       return new BitmapDrawable(getActivity().getResources(), bm);
}




public BitmapDrawable getBorder(int drawable ,String color){

Bitmap bMap = BitmapFactory.decodeResource(getResources(), drawable);

   final int BORDER_WIDTH = 1;
   final int BORDER_COLOR = Color.RED;
   Bitmap res = Bitmap.createBitmap(bMap.getWidth() + 2 * BORDER_WIDTH,
                                    bMap.getHeight() + 2 * BORDER_WIDTH,
                                    bMap.getConfig());
   Canvas c = new Canvas(res);
   Paint p = new Paint();
   p.setColor(BORDER_COLOR);
   c.drawRect(0, 0, res.getWidth(), res.getHeight(), p);
   p = new Paint(Paint.FILTER_BITMAP_FLAG);
   c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p);

   Matrix mat = new Matrix();
   mat.postRotate(350);
   Bitmap bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), mat, true);
   return new BitmapDrawable(getActivity().getResources(), bMapRotate);
}

Friday, January 25, 2013

Socialauth-android





SocialAuth Android is an Android version of popular SocialAuth Java library. Now you do not need to integrate multiple SDKs if you want to integrate your application with multiple social networks. You just need to add few lines of code after integrating the SocialAuth Android library in your app.
The API enables user authentication and sharing updates through different various social networks and hides all the intricacies of generating signatures & token, doing security handshakes and provide an easy mechanism to build cool social apps.
With this library, you can:
  • Quickly build share functionality for posting updates on facebook, twitter, linkedin and more
  • Easily create a Share button or a social bar containing various social networks
  • Access profile of logged in user for easy user registration
  • Import friend contacts of logged in user (Email, Profile URL and Name)
  • Do much more using our flexible API like extend it for more network
The SocialAuth Android SDK is a single download containing our library, ready examples to understand the functionality.

Friday, January 18, 2013

Uploading file from android to server


private void doFileUpload(){
              HttpURLConnection conn = null;
              DataOutputStream dos = null;
              DataInputStream inStream = null;
              String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mypic.png";
              String lineEnd = "\r\n";
              String twoHyphens = "--";
              String boundary =  "*****";
              int bytesRead, bytesAvailable, bufferSize;
              byte[] buffer;
              int maxBufferSize = 1*1024*1024;
              String responseFromServer = "";
              String urlString = "http://mywebsite.com/directory/upload.php";
              try
              {
               //------------------ CLIENT REQUEST
              FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
               // open a URL connection to the Servlet
               URL url = new URL(urlString);
               // Open a HTTP connection to the URL
               conn = (HttpURLConnection) url.openConnection();
               // Allow Inputs
               conn.setDoInput(true);
               // Allow Outputs
               conn.setDoOutput(true);
               // Don't use a cached copy.
               conn.setUseCaches(false);
               // Use a post method.
               conn.setRequestMethod("POST");
               conn.setRequestProperty("Connection", "Keep-Alive");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
               dos = new DataOutputStream( conn.getOutputStream() );
               dos.writeBytes(twoHyphens + boundary + lineEnd);
               dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
               dos.writeBytes(lineEnd);
               // create a buffer of maximum size
               bytesAvailable = fileInputStream.available();
               bufferSize = Math.min(bytesAvailable, maxBufferSize);
               buffer = new byte[bufferSize];
               // read file and write it into form...
               bytesRead = fileInputStream.read(buffer, 0, bufferSize);
               while (bytesRead > 0)
               {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
               }
               // send multipart form data necesssary after file data...
               dos.writeBytes(lineEnd);
               dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
               // close streams
               Log.e("Debug","File is written");
               fileInputStream.close();
               dos.flush();
               dos.close();
              }
              catch (MalformedURLException ex)
              {
                   Log.e("Debug", "error: " + ex.getMessage(), ex);
              }
              catch (IOException ioe)
              {
                   Log.e("Debug", "error: " + ioe.getMessage(), ioe);
              }
              //------------------ read the SERVER RESPONSE
              try {
                    inStream = new DataInputStream ( conn.getInputStream() );
                    String str;

                    while (( str = inStream.readLine()) != null)
                    {
                         Log.e("Debug","Server Response "+str);
                    }
                    inStream.close();

              }
              catch (IOException ioex){
                   Log.e("Debug", "error: " + ioex.getMessage(), ioex);
              }
            }
Things to note. 1) I had "mypic.png" within the root directory of the SD card. As in, if you looked at the Android device through Mass Storage USB view you would put the file in the first directory you come across.
2) USB MASS STORAGE MUST BE TURNED OFF ON THE PHONE! Or just completely unplug it from the computer you are writing the code on to assure this is the case.
3) I had to create an "uploads" folder in the same directory as my php file.
4) You obviously must change the web address I have written ashttp://mywebsite.com/directory/upload.php to be your own website.

Thursday, January 17, 2013


If you have a 4.2 Android powered device :
- Open the People/Contact
- Search for : "debug debug!"
- open the menu

Yeah, you can export contacts databases like a pro!