Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); imgView.setImageBitmap(imageBitmap); } }
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="true" /> <uses-feature android:name="android.hardware.camera.autofocus" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Camera Take Photo " android:layout_span="1" android:textAppearance="?android:attr/textAppearanceLarge" /> </TableRow> <View android:layout_height="1dip" android:background="#CCCCCC" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.1" android:orientation="horizontal" > <ImageView android:id="@+id/imgView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.75" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/btnTakePhoto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take Photo" /> </TableLayout> <View android:layout_height="1dip" android:background="#CCCCCC" /> <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="By.. ThaiCreate.Com" /> </LinearLayout> </TableLayout>
package com.myapp; import android.os.Bundle; import android.provider.MediaStore; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.view.View; import android.view.Menu; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { ImageView imgView; static final int REQUEST_IMAGE_CAPTURE = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //*** ImageView imgView = (ImageView) findViewById(R.id.imgView); //*** Take Photo final Button btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto); // Perform action on click btnTakePhoto.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); imgView.setImageBitmap(imageBitmap); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท