Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. public class Utility {
  2.  
  3. /**
  4. * @param inContext
  5. * @param inImage
  6. * @return
  7. */
  8. public static Uri getImageUri(Context inContext, Bitmap inImage) {
  9. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  10. inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  11. String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  12. return Uri.parse(path);
  13. }
  14.  
  15. /**
  16. *
  17. * @param uri
  18. * @param context
  19. * @return
  20. */
  21. public static String getRealPathFromURI(Uri uri ,Context context) {
  22. Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
  23. cursor.moveToFirst();
  24. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
  25. return cursor.getString(idx);
  26. }
  27.  
  28.  
  29.  
  30. /**
  31. *
  32. * @param context
  33. * @param resID
  34. * @return Uri from drawable
  35. */
  36. public static Uri getUriFromDrawable(Context context, int resID){
  37. return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
  38. context.getResources().getResourcePackageName(resID) + '/' +
  39. context.getResources().getResourceTypeName(resID) + '/' +
  40. context.getResources().getResourceEntryName(resID) );
  41. }
  42.  
  43.  
  44. /**
  45. *
  46. * @param context
  47. * @return true if connected to internet
  48. */
  49. public static boolean isNetworkAvailable(Context context) {
  50. try {
  51. ConnectivityManager cm = (ConnectivityManager)
  52. context.getSystemService(context.CONNECTIVITY_SERVICE);
  53. NetworkInfo networkInfo = cm.getActiveNetworkInfo();
  54. // if no network is available networkInfo will be null
  55. // otherwise check if we are connected
  56. if (networkInfo != null && networkInfo.isConnected()) {
  57. return true;
  58. }
  59. return false;
  60. } catch (Exception ex) {
  61. return false;
  62. }
  63. }
  64.  
  65.  
  66. /**
  67. *
  68. * @param millisecond
  69. * @return readable date
  70. */
  71. public static String getReadableDate(Long millisecond){
  72. Calendar calendar = Calendar.getInstance();
  73. calendar.setTimeInMillis(millisecond);
  74.  
  75. int mYear = calendar.get(Calendar.YEAR);
  76. int mMonth = calendar.get(Calendar.MONTH);
  77. int mDay = calendar.get(Calendar.MONTH);
  78.  
  79. return mYear+" " + mDay+" "+new SimpleDateFormat("MMM").format(calendar.getTime());
  80. }
  81.  
  82.  
  83. /**
  84. *
  85. * @param lat
  86. * @param lng
  87. * @param context
  88. * @return current location
  89. */
  90. public static String getAddress(double lat , double lng ,Context context){
  91. Geocoder gcd = new Geocoder(context, Locale.FRENCH);
  92. List<Address> addresses = null;
  93. try {
  94. addresses = gcd.getFromLocation(lat, lng, 1);
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. if (addresses.size() > 0)
  99. System.out.println(addresses.get(0).getLocality());
  100.  
  101. String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
  102. String city = addresses.get(0).getLocality();
  103. String state = addresses.get(0).getAdminArea();
  104. String country = addresses.get(0).getCountryName();
  105. String postalCode = addresses.get(0).getPostalCode();
  106. String knownName = addresses.get(0).getFeatureName();
  107.  
  108.  
  109. return address+" "+city+" "+state+" "+city;
  110. }
  111.  
  112.  
  113. /**
  114. * Launch another app if available or redirect user to playstore
  115. * @param context
  116. * @param packageName
  117. */
  118. public static void startNewActivity(Context context, String packageName) {
  119. Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
  120. if (intent != null) {
  121. // We found the activity now start the activity
  122. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  123. context.startActivity(intent);
  124. } else {
  125. // Bring user to the market or let them choose an app?
  126. intent = new Intent(Intent.ACTION_VIEW);
  127. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  128. intent.setData(Uri.parse("market://details?id=" + packageName));
  129. context.startActivity(intent);
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement