<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ashish Dangi</title>
    <description>The latest articles on DEV Community by Ashish Dangi (@ashishdangi).</description>
    <link>https://dev.to/ashishdangi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F866496%2F6118a21f-d96f-4bdf-8122-6bf46686c4cd.jpg</url>
      <title>DEV Community: Ashish Dangi</title>
      <link>https://dev.to/ashishdangi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashishdangi"/>
    <language>en</language>
    <item>
      <title>Geotags on Images in Android Studio (GeoTagImage)</title>
      <dc:creator>Ashish Dangi</dc:creator>
      <pubDate>Wed, 02 Aug 2023 03:00:34 +0000</pubDate>
      <link>https://dev.to/ashishdangi/geotags-on-images-in-android-studio-geotagimage-3829</link>
      <guid>https://dev.to/ashishdangi/geotags-on-images-in-android-studio-geotagimage-3829</guid>
      <description>&lt;p&gt;Hello everyone,&lt;/p&gt;

&lt;p&gt;In this blog I am going to guide you to how to add Geo tags or GPS location on the images clicked by the device camera. This is an android library to import in the android studio project and then just do some basic code. So here is the GeoTagImage Library for android app project.&lt;/p&gt;

&lt;p&gt;So First of all I am pointing out the key features of this library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request camera &amp;amp; location permission callback by itself.&lt;/li&gt;
&lt;li&gt;Show location and address.&lt;/li&gt;
&lt;li&gt;Show Lat Longs.&lt;/li&gt;
&lt;li&gt;Show author name.&lt;/li&gt;
&lt;li&gt;Show app name.&lt;/li&gt;
&lt;li&gt;Show Google map snapshot.&lt;/li&gt;
&lt;li&gt;Set Image Quality.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Handle Visibilities :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Show/hide app name — &lt;code&gt;showAppName()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Show/hide author name — &lt;code&gt;showAuthoraName()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Show/hide google map — &lt;code&gt;showGoogleMap()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Show/hide Lat Longs — &lt;code&gt;showLatLng()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So lets start, the brief summary of these points :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Request Camera &amp;amp; Location permission — The library can ask the permission by itself by using permission callback listener. Only the result need to handle in the activity or the fragment by using &lt;code&gt;OnRequestPermissionResult()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show Location &amp;amp; Address — The library can draw device’s current &lt;br&gt;
location such as sub-admin area, locality and country name along with complete address.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show Lat Longs — The library can also draw device’s current &lt;br&gt;
coordinates such as latitude and longitude.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show Author Name — The library can draw author (clicked by) name &lt;br&gt;
on the image. You need to add method &lt;code&gt;setAuthorName(“name”)&lt;/code&gt; and &lt;code&gt;showAuthorName(true)&lt;/code&gt; as by default the author name visibility is &lt;strong&gt;INVISIBLE&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show App Name — The library can draw app name also in which this&lt;br&gt;
library has been imported. You need to set visibility of this by using &lt;code&gt;showAppName(true)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show Google Map — The library can show google map snapshot of &lt;br&gt;
device’s current location along with marker. It depends on your project is linked with google map SDK or not. If not then the visibility of google map snapshot will be invisible by default.&lt;br&gt;
Set Image Quality — The library has a feature to set image quality. By default the image quality will be &lt;strong&gt;STANDARD&lt;/strong&gt; size and &lt;strong&gt;LOW&lt;/strong&gt; &amp;amp; &lt;strong&gt;HIGH&lt;/strong&gt; qualities are optional which can be use by &lt;code&gt;setImageQuality()&lt;/code&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Start Implementation of the library :&lt;/strong&gt;&lt;br&gt;
Gradle&lt;br&gt;
Add repository in your build.gradle (project-level) file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;allprojects {
      repositories {
 ...
 maven { url 'https://jitpack.io' }
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;strong&gt;in your settings.gradle&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add dependency :&lt;/strong&gt;&lt;br&gt;
Add dependency in your build.gradle (module-level) file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies{
    implementation 'com.github.dangiashish:GeoTagImage:1.0.7'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add file provider in AndroidManifest.xml :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;provider
      android:name="androidx.core.content.FileProvider"
      android:authorities="${applicationId}.provider"
      android:exported="false"
      android:grantUriPermissions="true"&amp;gt;
       &amp;lt;meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/provider_paths" 
&amp;lt;/provider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Create an xml file for path provider @xml/provider_path.xml :&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&amp;gt;
    &amp;lt;external-path name="external_files" path="."/&amp;gt;
&amp;lt;/paths&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Java : MainActivity.java :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MainActivity extends AppCompatActivity implements PermissionCallback {
    private ImageView ivCamera, ivImage, ivClose;
    private static String imageStoragePath;
    public static final String IMAGE_EXTENSION = ".jpg";
    private Uri fileUri;
    private static final int CAMERA_IMAGE_REQUEST_CODE = 2000;
    private static final int PERMISSION_REQUEST_CODE = 100;

    static FragmentActivity mContext;
    private GeoTagImage geoTagImage;
    private PermissionCallback permissionCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialize the context
        mContext = MainActivity.this;
        // initialize the permission callback listener
        permissionCallback = this;

        // initialize the GeoTagImage class object with context and callback
        // use try/catch block to handle exceptions.
        try {
            geoTagImage = new GeoTagImage(mContext, permissionCallback);
        } catch (GTIException e) {
            throw new RuntimeException(e);
        }

        // initialize the xml buttons.
        ivCamera = findViewById(R.id.ivCamera);
        ivImage = findViewById(R.id.ivImage);
        ivClose = findViewById(R.id.ivClose);

        // setOnClickListener on camera button.
        ivCamera.setOnClickListener(click -&amp;gt; {
            // first check permission for camera and location by using GTIPermission class.
            if (GTIPermissions.checkCameraLocationPermission(mContext)) {

                // if permissions are granted, than open camera.
                openCamera();

            } else {
                // otherwise request for the permissions by using GTIPermission class.
                GTIPermissions.requestCameraLocationPermission(mContext, PERMISSION_REQUEST_CODE);
            }
        });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;openCamera()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // if permissions are granted for camera and location.
    private void openCamera() {
        // call Intent for ACTION_IMAGE_CAPTURE which will redirect to device camera.
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // create a file object
        File file;

        // before adding GeoTags, generate or create an original image file
        // TODO-Note : we need to create an original image to add geotags by copying this file.
        file = GTIUtility.generateOriginalFile(mContext, IMAGE_EXTENSION);
        if (file != null) {
            // if file has been created, then will catch its path for future reference.
            imageStoragePath = file.getPath();
        }

        // now get Uri from this created image file by using GTIUtility.getFileUri() function.
        fileUri = GTIUtility.getFileUri(mContext, file);

        // pass this uri file into intent filters while opening camera.
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // call startActivityForResult method by passing the intent filter with a request code.
        startActivityForResult(intent, CAMERA_IMAGE_REQUEST_CODE);
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Handle request permission result:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length &amp;gt; 0 &amp;amp;&amp;amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                geoTagImage.handlePermissionGrantResult();
                Toast.makeText(mContext, "Permission Granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Handle onActivityResult method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// override the onActivityResult method
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // check the request code for the result
        if (requestCode == CAMERA_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                // if result is OK then preview the original created image file.
                previewCapturedImage();

                try {
                    // now call the function createImage() and pass the uri object (line no. 90-100)
                    geoTagImage.createImage(fileUri);

                    // set all the customizations for geotagging as per your requirements.
                    geoTagImage.setTextSize(25f);
                    geoTagImage.setBackgroundRadius(5f);
                    geoTagImage.setBackgroundColor(Color.parseColor("#66000000"));
                    geoTagImage.setTextColor(getColor(android.R.color.white));
                    geoTagImage.setAuthorName("Ashish");
                    geoTagImage.showAuthorName(true);
                    geoTagImage.showAppName(true);

                    // after the geotagged photo is created, get the new image path by using getImagePath() method
                    imageStoragePath = geoTagImage.getImagePath();


                } catch (GTIException e) {
                    throw new RuntimeException(e);
                }


                // handle the error or cancel events
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(mContext, "Cancelled image capture", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
            }
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Preview the original image&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     // preview of the original image
    private void previewCapturedImage() {
        try {
            ivCamera.setVisibility(View.GONE);
            Bitmap bitmap = GTIUtility.optimizeBitmap(imageStoragePath);
            ivImage.setImageBitmap(bitmap);

            if (ivImage.getDrawable() != null) {
                ivClose.setVisibility(View.VISIBLE);
            }
            ivClose.setOnClickListener(v -&amp;gt; {
                ivImage.setImageBitmap(null);
                ivCamera.setVisibility(View.VISIBLE);
                ivClose.setVisibility(View.GONE);
                ivImage.setImageDrawable(null);

            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Handle callback listener methods&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Override
public void onPermissionGranted() {
   openCamera();
}
@Override
public void onPermissionDenied() {
  GTIPermissions.requestCameraLocationPermission(mContext, PERMISSION_REQUEST_CODE);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you like it, Kindly Like ❤️ this post and give star to this repo on &lt;a href="https://github.com/dangiashish/GeoTagImage"&gt;Github&lt;/a&gt;. if you have any query or suggestion kindly ping me. Your feedbacks are appreciated.&lt;/p&gt;

&lt;p&gt;Follow on : &lt;a href="https://github.com/dangiashish"&gt;Github&lt;/a&gt;, &lt;a href="https://linkedin.com/in/ashishkumardangi"&gt;LinkedIn&lt;/a&gt;, &lt;a href="https://instagram.com/coder.ashish"&gt;Instagram&lt;/a&gt;&lt;br&gt;
Subscribe on youtube : &lt;a href="https://youtube.com/CodeByAshish"&gt;CodeByAshish&lt;/a&gt;&lt;br&gt;
Project Link : &lt;a href="https://github.com/dangiashish/GeoTagImage"&gt;GeoTagImage@github&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
