2014-03-06 33 views
0

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

回答

0

你爲什麼試圖使用元數據獲取圖片的位置(經度和緯度)?只需使用Android中的位置提供程序即可。 Here is the official documentation along with some code.

如果向下滾動到「用位置標記用戶創建的內容」部分,您會得到一些有用的建議。

讓我知道它是怎麼回事。歡呼聲

相關問題