Subversion Repository Public Repository

Nextrek

Diff Revisions 690 vs 696 for /Android/SmartCharging/SmartCharging/app/src/main/java/it/fedeloper/smartcharging/Manager/Utils.java

Diff revisions: vs.
  @@ -1,13 +1,18 @@
1 1 package it.fedeloper.smartcharging.Manager;
2 2
3 3 import android.content.Context;
4 + import android.database.Cursor;
4 5 import android.graphics.Bitmap;
5 6 import android.graphics.BitmapFactory;
6 7 import android.graphics.Canvas;
7 8 import android.graphics.Paint;
8 9 import android.net.ConnectivityManager;
9 10 import android.net.NetworkInfo;
11 + import android.net.Uri;
12 + import android.os.Debug;
13 + import android.provider.MediaStore;
10 14 import android.util.DisplayMetrics;
15 + import android.util.Log;
11 16 import android.view.Gravity;
12 17 import android.view.LayoutInflater;
13 18 import android.view.View;
  @@ -20,6 +25,7 @@
20 25 import java.io.InputStream;
21 26 import java.net.HttpURLConnection;
22 27 import java.net.InetAddress;
28 + import java.net.URI;
23 29 import java.net.URL;
24 30 import java.util.regex.Matcher;
25 31 import java.util.regex.Pattern;
  @@ -31,16 +37,6 @@
31 37 */
32 38 public class Utils {
33 39
34 - public static Bitmap getResizedBitmapFromStream(InputStream imageStream){
35 - BitmapFactory.Options options = new BitmapFactory.Options();
36 - options.inJustDecodeBounds = true;
37 - BitmapFactory.decodeStream(imageStream, null, options);
38 -
39 - options.inJustDecodeBounds = false;
40 -
41 - options.inSampleSize = calculateInSampleSize(options, 1000, 2000);
42 - return BitmapFactory.decodeStream(imageStream, null, options);
43 - }
44 40
45 41 public static int calculateInSampleSize(
46 42 BitmapFactory.Options options, int reqWidth, int reqHeight) {
  @@ -71,12 +67,14 @@
71 67 int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
72 68 return px;
73 69 }
70 +
74 71 public static boolean isValidEmailAddress(String email) {
75 72 String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
76 73 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
77 74 java.util.regex.Matcher m = p.matcher(email);
78 75 return m.matches();
79 76 }
77 +
80 78 public static Bitmap getBitmapFromURL(String src) {
81 79 try {
82 80 URL url = new URL(src);
  @@ -84,14 +82,57 @@
84 82 connection.setDoInput(true);
85 83 connection.connect();
86 84 InputStream input = connection.getInputStream();
87 - Bitmap myBitmap = BitmapFactory.decodeStream(input);
88 - return myBitmap;
85 +
86 + return decodeBitmapFromStreamFixed(input, 1000, 1000);
89 87 } catch (IOException e) {
90 88 e.printStackTrace();
91 89 return null;
92 90 }
93 91 }
94 92
93 +
94 + public static Bitmap decodeBitmapFromStreamFixed(InputStream inputStream, int reqWidth, int reqHeight) {
95 +
96 + byte[] byteArr = new byte[0];
97 + byte[] buffer = new byte[1024];
98 + int len;
99 + int count = 0;
100 +
101 + try {
102 + while ((len = inputStream.read(buffer)) > -1) {
103 + if (len != 0) {
104 + if (count + len > byteArr.length) {
105 + byte[] newbuf = new byte[(count + len) * 2];
106 + System.arraycopy(byteArr, 0, newbuf, 0, count);
107 + byteArr = newbuf;
108 + }
109 +
110 + System.arraycopy(buffer, 0, byteArr, count, len);
111 + count += len;
112 + }
113 + }
114 +
115 + final BitmapFactory.Options options = new BitmapFactory.Options();
116 + options.inJustDecodeBounds = true;
117 + BitmapFactory.decodeByteArray(byteArr, 0, count, options);
118 +
119 + options.inSampleSize = calculateInSampleSize(options, reqWidth,
120 + reqHeight);
121 + options.inPurgeable = true;
122 + options.inInputShareable = true;
123 + options.inJustDecodeBounds = false;
124 + options.inPreferredConfig = Bitmap.Config.ARGB_8888;
125 +
126 + return BitmapFactory.decodeByteArray(byteArr, 0, count, options);
127 +
128 + } catch (Exception e) {
129 + e.printStackTrace();
130 +
131 + return null;
132 + }
133 + }
134 +
135 +
95 136 public static Bitmap mergeBitmaps(Bitmap original, Bitmap overlay) {
96 137 Bitmap result = Bitmap.createBitmap(original.getWidth(), original
97 138 .getHeight(), Bitmap.Config.ARGB_8888);