1

我正在编写一个应用程序,它拍摄一张照片,然后显示图像及其元数据,这就是我目前所拥有的:

    <?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>

这是我的活动:

    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();

        }

    }  

我收到 imgFile 以及位置和位置的错误,说所有 3 个都无法解决。知道我写错了什么吗?我能做些什么来完成这项工作?谢谢!

4

1 回答 1

0

实际上,您没有在 onActivityResult() 中定义 imgFile 或位置。

您还需要导入位置:

import android.location.Location;

并且需要从 LocationManager 获取位置:

Context.getSystemService(Context.LOCATION_SERVICE);
于 2013-08-28T23:08:30.547 回答