public static String getRandomString(int length) {
final String s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
SecureRandom sr = new SecureRandom();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(s.charAt(sr.nextInt(s.length())));
}
return sb.toString();
}
public static void moveFile(File src, File dst) throws IOException {
if (dst.exists()) {
dst.delete();
}
InputStream input = new FileInputStream(src);
OutputStream output = new FileOutputStream(dst);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
src.delete();
}
public static void copyInputStreamToFile(InputStream src, File dst) throws IOException {
OutputStream output = new FileOutputStream(dst);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = src.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
@NonNull
public static String readTextFileFromAsset(Context c, String path) {
StringBuilder s = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = c.getResources().getAssets().open(path);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line;
while ((line = input.readLine()) != null) {
s.append(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return s.toString();
}
public static void openDialer(Context c, String number) {
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:" + number));
c.startActivity(i);
}
public static void copyFile(File src, File dst) throws IOException {
InputStream input = new FileInputStream(src);
OutputStream output = new FileOutputStream(dst);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
public static float pixelTodp(Context c, float pixel) {
float density = c.getResources().getDisplayMetrics().density;
float dp = pixel / density;
return dp;
}
public static float dpTopixel(Context c, float dp) {
float density = c.getResources().getDisplayMetrics().density;
float pixel = dp * density;
return pixel;
}
public static Bitmap rotateBitmap(Bitmap src, float rotationDegree) {
Matrix matrix = new Matrix();
matrix.setRotate(rotationDegree);
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
public static Bitmap flipBitmap(Bitmap src, int type/*0=horizontal, 1=vertical*/) {
Matrix matrix = new Matrix();
if(type == 0) {
matrix.preScale(-1.0f, 1.0f);
}
else if(type == 1) {
matrix.preScale(1.0f, -1.0f);
}
else {
return null;
}
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}
public static boolean isValidEmail(CharSequence target) {
if (TextUtils.isEmpty(target)) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
public static boolean isNetworkAvailable(Context c) {
ConnectivityManager connectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}