Skip to content

Commit ef35b3c

Browse files
committed
More Utilities
1 parent 27a79f8 commit ef35b3c

File tree

5 files changed

+359
-1
lines changed

5 files changed

+359
-1
lines changed

.DS_Store

2 KB
Binary file not shown.

AndroidServices/PingService.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class PingService extends Service {
2+
3+
4+
@Override
5+
public void onCreate() {
6+
super.onCreate();
7+
8+
}
9+
10+
public PingService() {
11+
super();
12+
13+
}
14+
15+
16+
@Nullable
17+
@Override
18+
public IBinder onBind(Intent intent) {
19+
return null;
20+
}
21+
22+
23+
@Override
24+
public int onStartCommand(Intent intent, int flags, int startId) {
25+
Log.e("Service Started","-->");
26+
handler.postDelayed(runnable, 1000*60*8);
27+
return super.onStartCommand(intent, flags, startId);
28+
}
29+
30+
31+
@Override
32+
public void onDestroy() {
33+
super.onDestroy();
34+
handler.removeCallbacks(runnable);
35+
}
36+
37+
@Override
38+
public void onLowMemory() {
39+
super.onLowMemory();
40+
}
41+
42+
private Handler handler = new Handler();
43+
private Runnable runnable = new Runnable() {
44+
@Override
45+
public void run() {
46+
Log.e("## - Service Continuing"," - ##");
47+
//Do Stuff
48+
//pingLocation(getApplicationContext(),"B");
49+
handler.postDelayed(this, 1000*60*8);
50+
}
51+
};
52+
53+
}
54+
55+
/** Calling **/
56+
startService(new Intent(this, PingService.class));
57+
stopService(new Intent(this, PingService.class));

CurrentLocation/MyLocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static abstract class LocationResult{
122122
}
123123

124124

125-
125+
/** Implementation Part **/
126126
MyLocation.LocationResult locationResult = new MyLocation.LocationResult(){
127127
@Override
128128
public void gotLocation(Location location){

ImageHandling/ImageHandling.java

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
2+
3+
/**
4+
* Insert image into SD card
5+
**/
6+
7+
public static void insertImageInSDCard(Bitmap bp, String name) {
8+
9+
/** Scale Down **/
10+
float maxImageSize = 1200;
11+
12+
float ratio = Math.min(
13+
(float) maxImageSize / bp.getWidth(),
14+
(float) maxImageSize / bp.getHeight());
15+
int width = Math.round((float) ratio * bp.getWidth());
16+
int height = Math.round((float) ratio * bp.getHeight());
17+
18+
Bitmap newBp = Bitmap.createScaledBitmap(bp, width, height, true);
19+
20+
File f = new File(Environment.getExternalStorageDirectory(), Config.MEDIA_FOLDER);
21+
if (!f.exists()) {
22+
f.mkdirs();
23+
}
24+
25+
File sdCardDirectory = Environment.getExternalStorageDirectory();
26+
File image = new File(sdCardDirectory.toString(), Config.MEDIA_FOLDER + name + ".jpg");
27+
if (image.exists()) {
28+
image.delete();
29+
}
30+
31+
// Encode the file as a PNG image.
32+
FileOutputStream outStream;
33+
try {
34+
outStream = new FileOutputStream(image);
35+
newBp.compress(Bitmap.CompressFormat.JPEG, 60, outStream);
36+
/* 100 to keep full quality of the image */
37+
38+
outStream.flush();
39+
outStream.close();
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
}
43+
44+
}
45+
46+
47+
48+
/**
49+
* Read image from external memory.
50+
**/
51+
52+
public static Bitmap readImageFromExternalMemory(String imageName) throws IOException {
53+
File file = new File(Environment.getExternalStorageDirectory().toString() + "/.nomedia/appName" + imageName + ".jpg");
54+
FileInputStream inStream = new FileInputStream(file);
55+
56+
Bitmap bitmap = BitmapFactory.decodeStream(inStream);
57+
inStream.close();
58+
59+
return bitmap;
60+
}
61+
62+
/**
63+
* Scale Down Bitmap
64+
**/
65+
66+
public static Bitmap scaledownBitmap(Bitmap bmp, int size) {
67+
68+
int height = size;
69+
70+
Bitmap background = Bitmap.createBitmap((int)height, (int)height, android.graphics.Bitmap.Config.ARGB_8888);
71+
72+
float originalWidth = bmp.getWidth();
73+
float originalHeight = bmp.getHeight();
74+
75+
Canvas canvas = new Canvas(background);
76+
77+
float scale = height / originalWidth;
78+
79+
float xTranslation = 0.0f;
80+
float yTranslation = (height - originalHeight * scale) / 2.0f;
81+
82+
Matrix transformation = new Matrix();
83+
transformation.postTranslate(xTranslation, yTranslation);
84+
transformation.preScale(scale, scale);
85+
86+
Paint paint = new Paint();
87+
paint.setFilterBitmap(true);
88+
89+
canvas.drawBitmap(bmp, transformation, paint);
90+
91+
return background;
92+
93+
}
94+
95+
96+
97+
public static Bitmap rotateImage(Bitmap source, float angle){
98+
Matrix matrix = new Matrix();
99+
matrix.postRotate(angle);
100+
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
101+
matrix, true);
102+
}
103+
104+
105+
106+
107+
108+
109+
/** Taking Image from Camera **/
110+
111+
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
112+
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFile());
113+
startActivityForResult(takePicture, image);
114+
115+
/** With Permission **/
116+
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(getActivity());
117+
if(!marshMallowPermission.checkPermissionForCamera()){
118+
marshMallowPermission.requestPermissionForCamera();
119+
}else {
120+
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
121+
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFile());
122+
startActivityForResult(takePicture, image);
123+
}
124+
125+
/** Image Path Uri **/
126+
127+
private Uri getOutputMediaFile(){
128+
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
129+
String imageFileName = timeStamp + ".jpg";
130+
File storageDir = Environment.getExternalStoragePublicDirectory(
131+
Environment.DIRECTORY_PICTURES);
132+
pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
133+
File file = new File(pictureImagePath);
134+
return Uri.fromFile(file);
135+
}
136+
137+
138+
139+
140+
/** Getting the Image **/
141+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
142+
super.onActivityResult(requestCode, resultCode, data);
143+
switch(requestCode) {
144+
case 1:
145+
146+
if(resultCode == RESULT_OK){
147+
try {
148+
Bitmap bitmap;
149+
File imgFile = new File(pictureImagePath);
150+
if(imgFile.exists()){
151+
/** For Camera **/
152+
bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
153+
}else{
154+
/** For from Gallery **/
155+
Uri pickedImage = data.getData();
156+
bitmap = decodeUri(pickedImage);
157+
}
158+
image2.setImageBitmap(bitmap);
159+
isImageView2Set = true;
160+
imageSetArray.add("img2");
161+
}catch (Exception e){
162+
e.printStackTrace();
163+
}
164+
165+
}
166+
default:
167+
break;
168+
}
169+
}
170+
171+
/** Decode Uri **/
172+
173+
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
174+
BitmapFactory.Options o = new BitmapFactory.Options();
175+
o.inJustDecodeBounds = true;
176+
BitmapFactory.decodeStream(
177+
getActivity().getContentResolver().openInputStream(selectedImage), null, o);
178+
179+
final int REQUIRED_SIZE = 100;
180+
181+
int width_tmp = o.outWidth, height_tmp = o.outHeight;
182+
int scale = 1;
183+
while (true) {
184+
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
185+
break;
186+
}
187+
width_tmp /= 2;
188+
height_tmp /= 2;
189+
scale *= 2;
190+
}
191+
192+
BitmapFactory.Options o2 = new BitmapFactory.Options();
193+
o2.inSampleSize = scale;
194+
return BitmapFactory.decodeStream(
195+
getActivity().getContentResolver().openInputStream(selectedImage), null, null);
196+
}
197+
198+
/** Stroing Image Example **/
199+
public void storingImages(){
200+
Bitmap imageBp;
201+
202+
int i = 1;
203+
if (imageSetArray.contains("img1")) {
204+
imageBp = ((BitmapDrawable) image1.getDrawable()).getBitmap();
205+
Util.insertImageInSDCard(imageBp, "ImageName" + "_Report" + i);
206+
i++;
207+
}
208+
if (imageSetArray.contains("img2")) {
209+
imageBp = ((BitmapDrawable) image2.getDrawable()).getBitmap();
210+
Util.insertImageInSDCard(imageBp, "ImageName" + "_Report" + i);
211+
i++;
212+
}
213+
if (imageSetArray.contains("img3")) {
214+
imageBp = ((BitmapDrawable) image3.getDrawable()).getBitmap();
215+
Util.insertImageInSDCard(imageBp, "ImageName" + "_Report" + i);
216+
i++;
217+
}
218+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
public class MarshMallowPermission {
2+
public static final int RECORD_PERMISSION_REQUEST_CODE = 1;
3+
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
4+
public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;
5+
public static final int REQUEST_LOCATION_PERMISSION = 4;
6+
7+
Activity activity;
8+
9+
public MarshMallowPermission(Activity activity) {
10+
this.activity = activity;
11+
}
12+
13+
public boolean checkPermissionForRecord(){
14+
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
15+
if (result == PackageManager.PERMISSION_GRANTED){
16+
return true;
17+
} else {
18+
return false;
19+
}
20+
}
21+
22+
public boolean checkPermissionForExternalStorage(){
23+
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
24+
if (result == PackageManager.PERMISSION_GRANTED){
25+
return true;
26+
} else {
27+
return false;
28+
}
29+
}
30+
31+
public boolean checkPermissionForCamera(){
32+
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
33+
if (result == PackageManager.PERMISSION_GRANTED){
34+
return true;
35+
} else {
36+
return false;
37+
}
38+
}
39+
40+
41+
public boolean checkPermissionForLocation(){
42+
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
43+
if (result == PackageManager.PERMISSION_GRANTED){
44+
return true;
45+
} else {
46+
return false;
47+
}
48+
}
49+
50+
51+
public void requestPermissionForRecord(){
52+
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)){
53+
Toast.makeText(activity, "Microphone permission needed for recording. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
54+
} else {
55+
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECORD_AUDIO},RECORD_PERMISSION_REQUEST_CODE);
56+
}
57+
}
58+
59+
public void requestPermissionForExternalStorage(){
60+
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
61+
Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
62+
} else {
63+
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
64+
}
65+
}
66+
67+
public void requestPermissionForCamera(){
68+
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){
69+
Toast.makeText(activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
70+
} else {
71+
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE);
72+
}
73+
}
74+
75+
76+
public void requestPermissionForLocation(){
77+
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)){
78+
Toast.makeText(activity, "Location permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
79+
} else {
80+
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_LOCATION_PERMISSION);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)