2012-05-11 32 views
0

我想創建一個應用程序,允許過敏的人拍攝他們的反應,當他們有一些過敏反應,以及他們吃的食物。然後他們可以將信息添加到文檔中,並保存所有這些信息,再加上地理定位。 (這個想法是過敏的人需要日記,因爲通常他可以每6個月看一次他的過敏科)我需要幫助來編碼和使用安卓相機

我已經寫了這段代碼:我必須點擊手機的菜單按鈕才能使用相機。但是,我現在想要一個允許我使用凸輪的按鈕。任何人都可以通過更改我的代碼幫助我,並幫助我正確添加一個按鈕allowind使用相機,以便我可以用我的android手機拍照嗎?

忠實,

我的代碼:

包android.camera;

 import android.app.Activity; 
     import android.content.Intent; 
     import android.graphics.Bitmap; 
     import android.os.Bundle; 
    import android.provider.MediaStore; 
     import android.view.Menu; 
     import android.view.MenuInflater; 
     import android.view.MenuItem; 
     import android.widget.ImageView; 



     public class CameraActivity extends Activity { 

      private static final int PICTURE_RESULT = 9; 

      private Bitmap mPicture; 
      private ImageView mView; 

      public void onCreate(Bundle icicle) { 
       super.onCreate(icicle); 
       setContentView(R.layout.main); 

       mView = (ImageView) findViewById(R.id.view); 
      } 

      @Override 
      public boolean onCreateOptionsMenu(Menu menu) { 
       MenuInflater inflater = getMenuInflater(); 
       inflater.inflate(R.menu.menu, menu); 
       return true; 
      } 

      @Override 
      public boolean onOptionsItemSelected(MenuItem item) { 
       switch (item.getItemId()) { 
       case R.id.camera: 
        Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        CameraActivity.this.startActivityForResult(camera, PICTURE_RESULT); 
        return true; 
       default: 
        return super.onOptionsItemSelected(item); 
       } 
      } 

    /*  public void onDestroy() { 
       super.onDestroy(); 
       if (mPicture != null) { 
        mPicture.recycle(); 
       } 
      }*/ 

      @Override 
      protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode, resultCode, data); 

       // if results comes from the camera activity 
       if (requestCode == PICTURE_RESULT) { 

        // if a picture was taken 
        if (resultCode == Activity.RESULT_OK) { 
         // Free the data of the last picture 
         if(mPicture != null) 
          mPicture.recycle(); 

         // Get the picture taken by the user 
         mPicture = (Bitmap) data.getExtras().get("data"); 

         // Avoid IllegalStateException with Immutable bitmap 
         Bitmap pic = mPicture.copy(mPicture.getConfig(), true); 
         mPicture.recycle(); 
         mPicture = pic; 

         // Show the picture 
         mView.setImageBitmap(mPicture); 

         // if user canceled from the camera activity 
        } else if (resultCode == Activity.RESULT_CANCELED) { 

        } 
       } 
      } 
     } 

回答

0

我覺得這是你在問什麼......

在您的佈局文件(main.xml中)添加一個按鈕:

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:text="Take Picture" android:id="@+id/take_picture"></Button> 

然後在您的onCreate方法:

public void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 
      setContentView(R.layout.main); 

      mView = (ImageView) findViewById(R.id.view); 
      mTakePictureButton = (Button) findViewById(R.id.take_picture); 
      mTakePictureButton.setClickable(true); 
      mTakePictureButton.setOnClickListener(new View.onClickListener(){ 

        @Override 
     public void onClick(View v) { 
      Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(camera, PICTURE_RESULT); 

     } 
      }); 
     } 
+0

是的!那很棒 !謝謝你,這是工作!但我想知道照片保存在哪裏?我希望我可以在同一個文檔中保存用戶的評論,圖片和地理定位。我應該將數據保存在文件中嗎?或者在我用Sqlite管理的本地數據庫中?有人可以幫我嗎? (我可能忘記了一天,我是一個初學者,但我喜歡代碼^^) –

+0

我相信你需要保存圖片。我不確定它是否得到保存。您可以運行此代碼,然後檢查您的畫廊以查看圖片是否已保存。爲了保存用戶的評論,圖片等,你應該管理一個sqlite數據庫。你可以在這裏看到一個例子:http://adblogcat.com/sqlite-database/ –

+0

謝謝你的例子。但我仍然有一個問題,是否有可能將圖片的用戶放在同一個本地數據庫中?我可以使用文件來保存用戶的數據嗎?請幫我 –