Question

I am having trouble trying to save an image taken by the camera and stored into the ImageView variable ImageBox. I am trying to save the image as 'Pic.jpg' and then using the ExifInterfaceclass, I would like to extract meta data from the image Pic.jpgand display its TAG_GPS_LATITUDE_REF and TAG_GPS_LONGITUDE_REF into the TextView variables LongitudeBoxand LatitudeBox. So far the application does load up fine and lets you take an image and displays the image into the ImageView ImageBox variable but! it does not display anything in either the LongitudeBox or LatitudeBox so I am not sure if it is unable to find the image or it cannot extract the data. Please help me find what's wrong within my code. Thank you!

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
    //variables
    Button CameraButton;
    ImageView ImageBox;
    TextView LatitudeBox, LongitudeBox;

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

        CameraButton = (Button) findViewById(R.id.CameraButton);
        ImageBox = (ImageView) findViewById(R.id.imageView1);

        LatitudeBox = (TextView) findViewById(R.id.LatitudeBox);
        LongitudeBox = (TextView) findViewById(R.id.LongitudeBox);

        //Camera Button Onclick
        CameraButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                Uri output=Uri.fromFile(photo);
                String IMG = "Pic.jpg";

                startActivityForResult(intent, 0);

                try {
                    ExifInterface exif = new ExifInterface(IMG);
                    LongitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF)); // get longitude
                    LatitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF)); //get latitude 

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }// try catch 
            }// end onclick 
        });// end camera button onclick





    }// close oncreate

    //display image inside ImageBox variable using bitmap
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == 0)
        {
        Bitmap TheImage = (Bitmap) data.getExtras().get("data");
        ImageBox.setImageBitmap(TheImage);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }







}// close main

No correct solution

OTHER TIPS

Why are you trying to get the location(latitude and longitude) of the picture using the metadata? Just use the location providers in Android. Here is the official documentation along with some code.

If you scroll down to the "Tagging user-created content with a location" section you'll get some helpful advice.

Let me know how it goes. Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top