Tuesday, 17 June 2014

how to copy one file to another file in android

public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}

cancel or clear push notifications in android

public static void cancelPushNotification(Context ctx, int notificationType) {
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationType);
}

how to take screenshot in android

public static void takeScreenShotForPage(RelativeLayout webView,
Context context, String path, String fileName) {
try {
webView.setDrawingCacheEnabled(true);

webView.buildDrawingCache(true);
Bitmap bitmap1 = null;
System.gc();
bitmap1 = Bitmap.createBitmap(webView.getDrawingCache(true));
Bitmap bmp = Common.getResizedBitmap(bitmap1, 150, 100);
webView.setDrawingCacheEnabled(false);

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

}

how to delete files in android

public static void deleteFile(File f) {
String[] flist = f.list();
for (int i = 0; i < flist.length; i++) {
File temp = new File(f.getAbsolutePath() + "/" + flist[i]);
if (temp.isDirectory()) {
deleteFile(temp);
temp.delete();
} else {
temp.delete();
}
}
f.delete();
}

how to use custom toast in android

public static void displayToast(Activity ctx, String title) {

LayoutInflater inflater = ctx.getLayoutInflater();
View layout = inflater.inflate(R.layout.customtoast,
(ViewGroup) ctx.findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(title);
Toast toast = new Toast(ctx);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(1500);
toast.setView(layout);
toast.show();
}

how to use shared preferences concepts in android

public static void saveStringPrefrence(String key, String value,
Context context) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
Editor edit = prefs.edit();
edit.putString(key, value);
edit.commit();
}


public static String getPrefrenceValue(String key, Context context) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String string = prefs.getString(key, "");
return string;
}

Reusable shared preference in android

public static void saveStringPrefrence(String key, String value,
Context context) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
Editor edit = prefs.edit();
edit.putString(key, value);
edit.commit();
}


public static String getPrefrenceValue(String key, Context context) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String string = prefs.getString(key, "");
return string;
}

How to convert the image which is in file to the bitmap in android

public static Bitmap decodeBitmap(File file) {
Bitmap bitmap = null;
// FileInputStream stream1;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inScaled = true;
// stream1 = new FileInputStream(file);
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), o);
// stream1.close();
} catch (Exception e) {
e.printStackTrace();
}

return bitmap;
}

how to convert the bitmap from the file url

public static Bitmap decodeBitmap(File file) {
Bitmap bitmap = null;
// FileInputStream stream1;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inScaled = true;
// stream1 = new FileInputStream(file);
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), o);
// stream1.close();
} catch (Exception e) {
e.printStackTrace();
}

return bitmap;
}

how to store bitmap in the particular file with user specified name in android

public static boolean storeImage(Bitmap imageData, String filename,
String iconStoragepath) {

File sdIconStorageDir = new File(iconStoragepath);
sdIconStorageDir.mkdirs();
try {
String filePath = sdIconStorageDir.toString() + "/" + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
imageData.compress(CompressFormat.PNG, 90, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

convert bitmap to .png formate in android

public static boolean storeImage(Bitmap imageData, String filename,
String iconStoragepath) {

File sdIconStorageDir = new File(iconStoragepath);
sdIconStorageDir.mkdirs();
try {
String filePath = sdIconStorageDir.toString() + "/" + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
imageData.compress(CompressFormat.PNG, 90, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

convert bitmap to .png formate in android

public static boolean storeImage(Bitmap imageData, String filename,
String iconStoragepath) {

File sdIconStorageDir = new File(iconStoragepath);
sdIconStorageDir.mkdirs();
try {
String filePath = sdIconStorageDir.toString() + "/" + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
imageData.compress(CompressFormat.PNG, 90, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

how to reduce the image size in android

public static Bitmap getResizedBitmap(Bitmap image, int newHeight,
int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}

how to decrese the size of bitmap in android

public static Bitmap getResizedBitmap(Bitmap image, int newHeight,
int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}

how to resize the bitmap in android

public static Bitmap getResizedBitmap(Bitmap image, int newHeight,
int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}

how to write single dialogue for all activities in android

public static void getAlertDialog(String title, String message, Context ctx) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(title);
builder.setMessage(message).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
return;
}
});
builder.create();
builder.show();
}

how to validate email in android

public static boolean isEmailValid(String email) {
// boolean isValid = false;
final Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9+._%-+]{1,256}" + "@" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+");

return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}

how to check the internet is available or not in android

public static boolean isOnline(Activity ctx) {
ConnectivityManager cm = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}

how to check network is available or not in android

public static boolean isOnline(Activity ctx) {
ConnectivityManager cm = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}