【发布时间】:2014-03-06 22:53:21
【问题描述】:
我在尝试保存由相机拍摄并存储到ImageView 变量ImageBox 中的图像时遇到问题。我正在尝试将图像保存为“Pic.jpg”,然后使用ExifInterfaceclass,我想从图像Pic.jpg 中提取元数据并将其TAG_GPS_LATITUDE_REF 和TAG_GPS_LONGITUDE_REF 显示到TextView变量LongitudeBox和LatitudeBox。到目前为止,该应用程序确实可以正常加载并允许您拍摄图像并将图像显示到ImageView ImageBox 变量中,但是!它在LongitudeBox 或LatitudeBox 中都没有显示任何内容,所以我不确定它是找不到图像还是无法提取数据。请帮我找出我的代码中有什么问题。谢谢!
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