Skip to content

Commit 39a2d12

Browse files
committed
1. Added code for getting location.
2. Added code for getting address of current location. 3. Added code for showing custom toast message. 4. Added code for getting file path. 5. Updated minSdkVersion to API 19.
1 parent 59bc8ab commit 39a2d12

36 files changed

+916
-1
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ group = 'com.github.amitjangid80'
66
android {
77
compileSdkVersion 27
88
defaultConfig {
9-
minSdkVersion 16
9+
minSdkVersion 19
1010
targetSdkVersion 27
1111
versionCode 1
1212
versionName "1.0"

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.amit.dbapilibrary">
33

4+
<uses-permission android:name="android.permission.INTERNET" />
45
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
7+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
58

69
<application
710
android:allowBackup="true"
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.amit.dbapilibrary.utilities;
2+
3+
import android.content.ContentUris;
4+
import android.content.Context;
5+
import android.database.Cursor;
6+
import android.net.Uri;
7+
import android.os.Build;
8+
import android.os.Environment;
9+
import android.provider.DocumentsContract;
10+
import android.provider.MediaStore;
11+
12+
/**
13+
* Created by AmitS on 15-05-2017.
14+
*/
15+
16+
public class FilePath
17+
{
18+
/**
19+
* Method for return file path of Gallery image/ Document / Video / Audio
20+
*
21+
* @param context
22+
* @param uri
23+
* @return - path of the selected image file from gallery
24+
*/
25+
public static String getPath(final Context context, final Uri uri)
26+
{
27+
// check here to KITKAT or new version
28+
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
29+
30+
// DocumentProvider
31+
if (isKitKat && DocumentsContract.isDocumentUri(context, uri))
32+
{
33+
// ExternalStorageProvider
34+
if (isExternalStorageDocument(uri))
35+
{
36+
final String docId = DocumentsContract.getDocumentId(uri);
37+
final String[] split = docId.split(":");
38+
final String type = split[0];
39+
40+
if ("primary".equalsIgnoreCase(type))
41+
{
42+
return Environment.getExternalStorageDirectory() + "/"
43+
+ split[1];
44+
}
45+
}
46+
// DownloadsProvider
47+
else if (isDownloadsDocument(uri))
48+
{
49+
final String id = DocumentsContract.getDocumentId(uri);
50+
51+
final Uri contentUri = ContentUris.withAppendedId(
52+
Uri.parse("content://downloads/public_downloads"),
53+
Long.valueOf(id));
54+
55+
return getDataColumn(context, contentUri, null, null);
56+
}
57+
// MediaProvider
58+
else if (isMediaDocument(uri))
59+
{
60+
final String docId = DocumentsContract.getDocumentId(uri);
61+
final String[] split = docId.split(":");
62+
final String type = split[0];
63+
64+
Uri contentUri = null;
65+
66+
if ("image".equals(type))
67+
{
68+
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
69+
}
70+
else if ("video".equals(type))
71+
{
72+
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
73+
}
74+
else if ("audio".equals(type))
75+
{
76+
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
77+
}
78+
79+
final String selection = "_id=?";
80+
final String[] selectionArgs = new String[] { split[1] };
81+
82+
return getDataColumn(context, contentUri, selection,
83+
selectionArgs);
84+
}
85+
}
86+
// MediaStore (and general)
87+
else if ("content".equalsIgnoreCase(uri.getScheme()))
88+
{
89+
// Return the remote address
90+
if (isGooglePhotosUri(uri))
91+
return uri.getLastPathSegment();
92+
93+
return getDataColumn(context, uri, null, null);
94+
}
95+
// File
96+
else if ("file".equalsIgnoreCase(uri.getScheme()))
97+
{
98+
return uri.getPath();
99+
}
100+
101+
return null;
102+
}
103+
104+
/**
105+
* Get the value of the data column for this Uri. This is useful for
106+
* MediaStore Uris, and other file-based ContentProviders.
107+
*
108+
* @param context - The context.
109+
* @param uri - The Uri to query.
110+
* @param selection - (Optional) Filter used in the query.
111+
* @param selectionArgs - (Optional) Selection arguments used in the query.
112+
* @return - The value of the _data column, which is typically a file path.
113+
*/
114+
private static String getDataColumn(Context context, Uri uri,
115+
String selection, String[] selectionArgs)
116+
{
117+
Cursor cursor = null;
118+
final String column = "_data";
119+
final String[] projection = { column };
120+
121+
try
122+
{
123+
cursor = context.getContentResolver().query(uri, projection,
124+
selection, selectionArgs, null);
125+
126+
if (cursor != null && cursor.moveToFirst())
127+
{
128+
final int index = cursor.getColumnIndexOrThrow(column);
129+
return cursor.getString(index);
130+
}
131+
}
132+
finally
133+
{
134+
if (cursor != null)
135+
cursor.close();
136+
}
137+
138+
return null;
139+
}
140+
141+
/**
142+
* @param uri - The Uri to check.
143+
* @return - Whether the Uri authority is ExternalStorageProvider.
144+
*/
145+
private static boolean isExternalStorageDocument(Uri uri)
146+
{
147+
return "com.android.externalstorage.documents".equals(uri.getAuthority());
148+
}
149+
150+
/**
151+
* @param uri - The Uri to check.
152+
* @return - Whether the Uri authority is DownloadsProvider.
153+
*/
154+
private static boolean isDownloadsDocument(Uri uri)
155+
{
156+
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
157+
}
158+
159+
/**
160+
* @param uri - The Uri to check.
161+
* @return - Whether the Uri authority is MediaProvider.
162+
*/
163+
private static boolean isMediaDocument(Uri uri)
164+
{
165+
return "com.android.providers.media.documents".equals(uri.getAuthority());
166+
}
167+
168+
/**
169+
* @param uri - The Uri to check.
170+
* @return - Whether the Uri authority is Google Photos.
171+
*/
172+
private static boolean isGooglePhotosUri(Uri uri)
173+
{
174+
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
175+
}
176+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.amit.dbapilibrary.utilities;
2+
3+
import android.content.Context;
4+
import android.location.Address;
5+
import android.location.Geocoder;
6+
import android.os.Bundle;
7+
import android.os.Handler;
8+
import android.os.Message;
9+
import android.util.Log;
10+
11+
import java.util.List;
12+
import java.util.Locale;
13+
14+
/**
15+
* 2018 April 21 - Saturday - 04:27 PM
16+
* Location Address class
17+
*
18+
* this class helps to get the address of the current location
19+
* using the latitude and longitude provided
20+
**/
21+
public class LocationAddress
22+
{
23+
private static final String TAG = LocationAddress.class.getSimpleName();
24+
25+
/**
26+
* 2018 April 21 - Saturday - 04:28 PM
27+
* Get Address from location method
28+
*
29+
* this method will get the address from current location
30+
* using the latitude and longitude and
31+
* will return the complete address of current location
32+
**/
33+
public static void getAddressFromLocation(final double latitude, final double longitude,
34+
final Context context, final Handler handler)
35+
{
36+
Thread thread = new Thread()
37+
{
38+
@Override
39+
public void run()
40+
{
41+
String result;
42+
Address address = null;
43+
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
44+
45+
try
46+
{
47+
// getting the list of address from latitude and longitude
48+
List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
49+
50+
if (addressList != null && addressList.size() > 0)
51+
{
52+
address = addressList.get(0);
53+
}
54+
}
55+
catch (Exception e)
56+
{
57+
Log.e(TAG, "getAddressFromLocation:run: exception while getting address from location");
58+
e.printStackTrace();
59+
}
60+
finally
61+
{
62+
Message message = Message.obtain();
63+
message.setTarget(handler);
64+
65+
if (address != null)
66+
{
67+
message.what = 1;
68+
Bundle bundle = new Bundle();
69+
70+
bundle.putString("thoroughFare", address.getThoroughfare());
71+
bundle.putString("subThoroughFare", address.getSubThoroughfare());
72+
bundle.putString("city", address.getLocality());
73+
bundle.putString("state", address.getAdminArea());
74+
bundle.putString("country", address.getCountryName());
75+
bundle.putString("postalCode", address.getPostalCode());
76+
bundle.putString("subAdminArea", address.getSubAdminArea());
77+
bundle.putString("subLocality", address.getSubLocality());
78+
message.setData(bundle);
79+
}
80+
else
81+
{
82+
message.what = 1;
83+
Bundle bundle = new Bundle();
84+
85+
result = "Latitude: " + latitude + "Longitude: " + longitude +
86+
"\n Unable to get address for this location.";
87+
88+
bundle.putString("address", result);
89+
message.setData(bundle);
90+
}
91+
92+
message.sendToTarget();
93+
}
94+
}
95+
};
96+
97+
thread.start();
98+
}
99+
}

0 commit comments

Comments
 (0)