Friday, September 21, 2012

SENDING FILE TO WEB SERVER USING ANDROID


FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(image);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     
           bitmap=BitmapFactory.decodeStream(fileInputStream);
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
           byte[] data = baos.toByteArray();
           IMAGE= Base64.encodeBytes(data);
           Log.e("THE IMAGE BYTES",""+IMAGE);




IMAGE IS STRING TO SEND TO WEB SERVER 

Thursday, September 20, 2012

how to find file path extension and size not exceeded to 1 mb in android





String s="file path";
File file = new File(s);
   
long sizeInBytes = file.length();
sizeInBytes = sizeInBytes/1024;
Log.e("login bytes", ""+sizeInBytes);
     //transform in MB
     //double sizeInMb = sizeInBytes/(1024*1024);
               
     if(sizeInBytes<=2048)
   
        {
    Log.e("login", ""+sizeInBytes);
      int dot = c.lastIndexOf(".");
      String ff= c.substring(dot+1);
         
    //  tv1.setText(ff);
    if(ff=="pdf"||ff.compareTo("txt")==0||ff=="docx"||ff=="doc"||ff=="xls"||ff=="xlsx"||ff=="ppt"||ff=="pptx")
      {
    //Log.e("login if", ""+ff);
        exsistingFileName=c;
        Log.e("path file", ""+exsistingFileName);
    String searchFor = "bit.ly/yyyyyy";
    String s1=feed1.getText().toString();
        int len = searchFor.length();
        int result = 0;
        if (len > 0) {
        int start = s1.indexOf(searchFor);
        while (start != -1) {
        result++;
        start =s1.indexOf(searchFor, start+len);                
             
        }
        }        
       if(result==0)
    {
    feed1.append(" bit.ly/yyyyyy ");
    }
       else
       {
     
       }
     
      }
    else
    {
    showAlertDialog("file type only doc,txt,doc,docx,ppt,xls,xls");
    }

//Log.e("path", ""+exsistingFileName);
        }
    else
    {
    showAlertDialog("file size not exceed to 2mb");
    }

}

how to use bit.ly url in ur android program


int leng=ulink.length();
     
         for (int i = 0;  i <= (leng - 7);i++) {
                if (ulink.regionMatches(i, "http://", 0, 6)) {
                youtubelink=ulink;}}
         for (int i = 0;  i <= (leng - 4);i++) {
                if (ulink.regionMatches(i, "www.", 0, 3)) {
                youtubelink=ulink.replace("www.", "http://");
                youtubelink=youtubelink.trim();}}  
       try{
   
          url = as("user name", "api key").call(shorten(youtubelink));
          String s1=url.getShortUrl();
          feed1.append(" "+s1);
       }
       catch(Exception e)
       {
        showAlertDialog("please check your url and its wrong");
       }
       
     
         // Do something with value!
         }




public class BitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
     
       
        Url url = as("username", "key").call(shorten("http://google.com"));
        String s=url.getShortUrl();
        Log.e("url",s);
   
        URLConnection conn = null;
       
       conn =  connectURL(s);
     
       String sss=conn.toString();
       Log.e("expand",sss);

           
    }

private URLConnection connectURL(String s) {
// TODO Auto-generated method stub
URLConnection conn =null;
try {
        URL inputURL = new URL(s);
        conn = inputURL.openConnection();
             //  int test = 0;

    }catch(MalformedURLException e) {
        System.out.println("Please input a valid URL");
    }catch(IOException ioe) {
        System.out.println("Can not connect to the URL");
    }
return conn;
}





its very simple
       

how to get a device id in android phone


 final TelephonyManager tm = (TelephonyManager) this
  .getSystemService(Context.TELEPHONY_SERVICE);

  final String tmDevice, tmSerial, androidId;
  tmDevice = "" + tm.getDeviceId();
  tmSerial = "" + tm.getSimSerialNumber();
  androidId = ""
  + android.provider.Settings.Secure.getString(this
  .getContentResolver(),
  android.provider.Settings.Secure.ANDROID_ID);

  UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice
  .hashCode() << 32)
  | tmSerial.hashCode());
  DID = deviceUuid.toString();


important user permission is  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>