Android获取捕获的图像元数据

时间:2014-03-02 01:31:10

标签: java android exif

我正在尝试捕获通过相机捕获并存储到ImageBox变量中的图像的纬度。使用ExifInterface类,我试图获取图像的纬度并将该信息显示到LatitudeBox变量但我的语法不正确所以请让我知道我做错了什么!完整的代码

MainActivity.java

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
    //variables
    Button CameraButton;
    ImageView ImageBox;
    EditText LatitudeBox, LongitudeBox;
    String IMG = ImageBox.toString();

    @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 = (EditText) findViewById(R.id.LatitudeBox);
        LongitudeBox = (EditText) 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);
                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

应用程序现在在启动时崩溃,但不确定是什么导致它。这是logcat错误的图像

http://s7.postimg.org/4p67papyj/logcat1.png

activity_main.XML

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="250dp"
    android:layout_height="200dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/CameraButton"
    android:layout_width="150dp"
    android:layout_height="35dp"
    android:layout_below="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="38dp"
    android:text="@string/Camera_Button" />

<TextView
    android:id="@+id/LatitudeBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/CameraButton"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:text="@string/LatitudeBox"
    android:textColor="#ff0000"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/LongitudeBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/LatitudeBox"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="21dp"
    android:text="@string/LongitudeBox"
    android:textAppearance="?android:attr/textAppearanceLarge" />

1 个答案:

答案 0 :(得分:1)

ExifInterface没有.get()方法。

LatitudeBox = exif.get(ExifInterface.TAG_GPS_LONGITUDE_REF);

你的意思是:

LatitudeBox.setText(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF));

编辑:

我找到了。

在这里查看最后一个变量:

Button CameraButton;
ImageView ImageBox;
EditText LatitudeBox, LongitudeBox;
String IMG = ImageBox.toString();

您尚未为ImageView ImageBox分配值。因此,当您ImageBox.toString()时,您会收到NullPointerException

将IMG的初始化更改为:

String IMG = "filename.txt";