用android拍照和显示元数据

时间:2013-08-28 22:33:48

标签: android android-intent metadata android-camera

我正在编写一个带照片的应用程序,然后显示带有元数据的图像,这是我到目前为止所做的:

    <?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都无法解析。知道我编​​码错了吗?我能做些什么来完成这项工作? 谢谢!

1 个答案:

答案 0 :(得分:0)

事实上,您没有在onActivityResult()中定义imgFile或location。

您还需要导入位置:

import android.location.Location;

需要从以下位置获取LocationManager的位置:

Context.getSystemService(Context.LOCATION_SERVICE);