Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Utility {
- /**
- * @param inContext
- * @param inImage
- * @return
- */
- public static Uri getImageUri(Context inContext, Bitmap inImage) {
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
- String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
- return Uri.parse(path);
- }
- /**
- *
- * @param uri
- * @param context
- * @return
- */
- public static String getRealPathFromURI(Uri uri ,Context context) {
- Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
- cursor.moveToFirst();
- int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
- return cursor.getString(idx);
- }
- /**
- *
- * @param context
- * @param resID
- * @return Uri from drawable
- */
- public static Uri getUriFromDrawable(Context context, int resID){
- return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
- context.getResources().getResourcePackageName(resID) + '/' +
- context.getResources().getResourceTypeName(resID) + '/' +
- context.getResources().getResourceEntryName(resID) );
- }
- /**
- *
- * @param context
- * @return true if connected to internet
- */
- public static boolean isNetworkAvailable(Context context) {
- try {
- ConnectivityManager cm = (ConnectivityManager)
- context.getSystemService(context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = cm.getActiveNetworkInfo();
- // if no network is available networkInfo will be null
- // otherwise check if we are connected
- if (networkInfo != null && networkInfo.isConnected()) {
- return true;
- }
- return false;
- } catch (Exception ex) {
- return false;
- }
- }
- /**
- *
- * @param millisecond
- * @return readable date
- */
- public static String getReadableDate(Long millisecond){
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(millisecond);
- int mYear = calendar.get(Calendar.YEAR);
- int mMonth = calendar.get(Calendar.MONTH);
- int mDay = calendar.get(Calendar.MONTH);
- return mYear+" " + mDay+" "+new SimpleDateFormat("MMM").format(calendar.getTime());
- }
- /**
- *
- * @param lat
- * @param lng
- * @param context
- * @return current location
- */
- public static String getAddress(double lat , double lng ,Context context){
- Geocoder gcd = new Geocoder(context, Locale.FRENCH);
- List<Address> addresses = null;
- try {
- addresses = gcd.getFromLocation(lat, lng, 1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (addresses.size() > 0)
- System.out.println(addresses.get(0).getLocality());
- String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
- String city = addresses.get(0).getLocality();
- String state = addresses.get(0).getAdminArea();
- String country = addresses.get(0).getCountryName();
- String postalCode = addresses.get(0).getPostalCode();
- String knownName = addresses.get(0).getFeatureName();
- return address+" "+city+" "+state+" "+city;
- }
- /**
- * Launch another app if available or redirect user to playstore
- * @param context
- * @param packageName
- */
- public static void startNewActivity(Context context, String packageName) {
- Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
- if (intent != null) {
- // We found the activity now start the activity
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intent);
- } else {
- // Bring user to the market or let them choose an app?
- intent = new Intent(Intent.ACTION_VIEW);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setData(Uri.parse("market://details?id=" + packageName));
- context.startActivity(intent);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement