【问题标题】:Android save image to file and use ExifInterface to extract meta dataAndroid 将图像保存到文件并使用 ExifInterface 提取元数据
【发布时间】:2014-03-06 22:53:21
【问题描述】:

我在尝试保存由相机拍摄并存储到ImageView 变量ImageBox 中的图像时遇到问题。我正在尝试将图像保存为“Pic.jpg”,然后使用ExifInterfaceclass,我想从图像Pic.jpg 中提取元数据并将其TAG_GPS_LATITUDE_REFTAG_GPS_LONGITUDE_REF 显示到TextView变量LongitudeBoxLatitudeBox。到目前为止,该应用程序确实可以正常加载并允许您拍摄图像并将图像显示到ImageView ImageBox 变量中,但是!它在LongitudeBoxLatitudeBox 中都没有显示任何内容,所以我不确定它是找不到图像还是无法提取数据。请帮我找出我的代码中有什么问题。谢谢!

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

【问题讨论】:

    标签: android image camera imageview exif


    【解决方案1】:

    您为什么要尝试使用元数据获取图片的位置(纬度和经度)?只需使用 Android 中的位置提供程序。 Here is the official documentation along with some code.

    如果您向下滚动到“使用位置标记用户创建的内容”部分,您会得到一些有用的建议。

    让我知道进展如何。干杯

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      相关资源
      最近更新 更多