我無法保存相機拍攝的圖像並將其存儲到ImageView
變量ImageBox
中。我試圖將圖像保存爲'Pic.jpg',然後使用ExifInterface
類,我想從圖像中提取元數據Pic.jpg
並將其TAG_GPS_LATITUDE_REF
和TAG_GPS_LONGITUDE_REF
顯示爲TextView
變量LongitudeBox
和LatitudeBox
。到目前爲止,應用程序的加載效果並不理想,可讓您拍攝圖像並將圖像顯示在ImageView ImageBox
變量中,但!它不會在LongitudeBox
或LatitudeBox
中顯示任何內容,因此我不確定它是否無法找到圖像或無法提取數據。請幫我看看我的代碼中有什麼問題。謝謝!Android將圖像保存到文件並使用ExifInterface提取元數據
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