Taking a photo with android and displaying metadat

2019-03-02 06:30发布

I am writing an app that takes a photo and then displays the image with its metadata, this is what I have so far:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/openCamera" />
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="fill_parent"
            android:layout_height="300.0dp"
            android:id="@+id/imageView1"
            android:adjustViewBounds="true" />
        </LinearLayout>

And here is my activity:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Calendar;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Camera;
    import android.graphics.Matrix;
    import android.media.ExifInterface;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainActivity extends Activity {
        private static final int CAMERA_REQUEST = 1888; 
        private ImageView imageView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.imageView = (ImageView)this.findViewById(R.id.imageView1);
            Button photoButton = (Button) this.findViewById(R.id.button1);
            photoButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent cameraIntent = new              Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }

    ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());
    //String latitudeStr = "90/1,12/1,30/1";
    double lat = location.getLatitude();
    double alat = Math.abs(lat);
    String dms = Location.convert(alat, Location.FORMAT_SECONDS);
    String[] splits = dms.split(":");
    String[] secnds = (splits[2]).split("\\.");
    String seconds;
    if(secnds.length==0)
    {
        seconds = splits[2];
    }
    else
    {
        seconds = secnds[0];
    }

    String latitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitudeStr);

    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, lat>0?"N":"S");

    double lon = location.getLongitude();
    double alon = Math.abs(lon);


    dms = Location.convert(alon, Location.FORMAT_SECONDS);
    splits = dms.split(":");
    secnds = (splits[2]).split("\\.");

    if(secnds.length==0)
    {
        seconds = splits[2];
    }
    else
    {
        seconds = secnds[0];
    }
    String longitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";


    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitudeStr);
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, lon>0?"E":"W");

    exif.saveAttributes();

}
            ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());
            //String latitudeStr = "90/1,12/1,30/1";
            double lat = location.getLatitude();
            double alat = Math.abs(lat);
            String dms = Location.convert(alat, Location.FORMAT_SECONDS);
            String[] splits = dms.split(":");
            String[] secnds = (splits[2]).split("\\.");
            String seconds;
            if(secnds.length==0)
            {
                seconds = splits[2];
            }
            else
            {
                seconds = secnds[0];
            }

            String latitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitudeStr);

            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, lat>0?"N":"S");

            double lon = location.getLongitude();
            double alon = Math.abs(lon);


            dms = Location.convert(alon, Location.FORMAT_SECONDS);
            splits = dms.split(":");
            secnds = (splits[2]).split("\\.");

            if(secnds.length==0)
            {
                seconds = splits[2];
            }
            else
            {
                seconds = secnds[0];
            }
            String longitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";


            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitudeStr);
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, lon>0?"E":"W");

            exif.saveAttributes();

        }

    }  

I am getting an error for imgFile and with location and Location, saying all 3 cannot be resolved. Any idea what I have coded wrong? What can I do to make this work? Thanks!

1条回答
仙女界的扛把子
2楼-- · 2019-03-02 07:09

In fact you are not defining imgFile or location in onActivityResult().

Also you need to import Location:

import android.location.Location;

And need to get the location from a LocationManager obtained from:

Context.getSystemService(Context.LOCATION_SERVICE);
查看更多
登录 后发表回答