Archive | Android RSS for this section

How to convert bitmap to byte array in android and java

/**
 * @param bitmap
 * Bitmap object from which you want to get bytes
 * @return byte[] array of bytes by compressing the bitmap to PNG format <br/>
 * null if bitmap passed is null (or) failed to get bytes from the
 * bitmap
 */
 public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
 if (bitmap == null) {
 return null;
 } else {
 byte[] b = null;
 try {
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 bitmap.compress(CompressFormat.PNG, 0, byteArrayOutputStream);
 b = byteArrayOutputStream.toByteArray();
 } catch (Exception e) {
 e.printStackTrace();
 }
 return b;
 }
 }

how to get / find your device IMEI number programmatically in andorid


TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String IMEI_Number = telephonyManager.getDeviceId();
Log.d("your device IMEI number -->",IMEI_Number);

Requires permission android.permission.READ_PHONE_STATE.
Dial *#06# to know your IMEI Number or Remove your battery and look for IMEI,you will find it.

How to get / find your android device id or serial number programmatically


String device_id = Build.SERIAL; /* This string is available from api level 9 */
Log.d("your device id or serial number -->", device_id);

how to get / find UUID of an android device programmatically


 String device_uuid = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);
 Log.d("your device uuid --> ", device_uuid);

How to convert android project to executable .jar file

Right Click on Project –> Export —> Java –> JarFile

Window will appear Select Resources to Export

Select only the src checkbox ie the (pacakages) in Top-Left Window.

Don’t Select anything in Top-Right Window if some thing is selected  remove the tick mark

Select only the first Checkbox in the middle left window ie  Export generates Class Files and Resources

Select the Export Destination ie the path where u want to save ur jar file

In Options Select only Compress the contents of the jar file

Click Finish u will find ur jar file

Now if u want to add this jar file to any project. It is very simple

Right click on project –> Select Properties –> Java Build Path —> libraries –> Add External Jar Files –> Click OK

Now just Build your project U WILL find the jar file

How to know/find the version of jdk/java which is installed in linux/ubuntu system

Open Terminal and type

java -version

or

javac -version

how to know find the path of java/jdk in ubuntu/linux

 /usr/lib/jvm  is the path where the jdk is located.

How to calculate distance between any two geo points (latitudes and longitudes) in android


/**
 * @param fromLatitude
 * @param fromLongitude
 * @param toLatitude
 * @param toLongitude
 * @return distance between the two latitudes and longitudes in kms.
 */
 public static double calculateDistance(double fromLatitude,double fromLongitude,double toLatitude,double toLongitude)
 {

 float results[] = new float[1];

try {
 Location.distanceBetween(fromLatitude,fromLongitude, toLatitude, toLongitude, results);
 } catch (Exception e) {
 if (e != null)
 e.printStackTrace();
 }

 int dist = (int) results[0];
 if(dist<=0)
 return 0D;

 DecimalFormat decimalFormat = new DecimalFormat("#.##");
 results[0]/=1000D;
 String distance = decimalFormat.format(results[0]);
 double d = Double.parseDouble(distance);
 return d;
 }

How to get no of days difference between any two dates in java/android

Date as parameter

 public static int getDaysDifference(Date fromDate,Date toDate)
 {
 if(fromDate==null||toDate==null)
 return 0;

 return (int)( (toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
 }
 

Calendar as parameter

 public static int getDaysDifference(Calendar calendar1,Calendar calendar2)
 {
 if(calendar1==null||calendar2==null)
 return 0;

 return (int)( (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60 * 24));
 }
 

How to get total number of days in a month in java/android

In a particular year

/**
 * @param monthNumber Month Number starts with 0. For <b>January</b> it is <b>0</b> and for <b>December</b> it is <b>11</b>.
 * @param year
 * @return
 */
 public static int getDaysInMonth(int monthNumber,int year)
 {
 int days=0;
 if(monthNumber>=0 && monthNumber<12){
 try
 {
 Calendar calendar = Calendar.getInstance();
 int date = 1;
 calendar.set(year, monthNumber, date);
 days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 } catch (Exception e)
 {
 if(e!=null)
 e.printStackTrace();
 }
 }
 return days;
 }

In  current year


/**
 * @param monthNumber Month Number starts with 0. For <b>January</b> it is <b>0</b> and for <b>December</b> it is <b>11</b>.
 * @return
 */
 public static int getDaysInMonthInPresentYear(int monthNumber)
 {
 int days=0;
 if(monthNumber>=0 && monthNumber<12){
 try
 {
 Calendar calendar = Calendar.getInstance();
 int date = 1;
 int year = calendar.get(Calendar.YEAR);
 calendar.set(year, monthNumber, date);
 days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 } catch (Exception e)
 {
 if(e!=null)
 e.printStackTrace();
 }
 }
 return days;
 }