activity_mail.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/cameraButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take Picture" /> <ImageView android:id="@+id/snapshotImageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> </LinearLayout>
MainActivity.java
package com.blogspot.kantapp.cameraexampleinandroid; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { Button b; ImageView img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.snapshotImageView); b = (Button) findViewById(R.id.cameraButton); b.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub Intent in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(in, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { Bitmap image = (Bitmap) data.getExtras().get("data"); img.setImageBitmap(image); } else if (resultCode == RESULT_CANCELED) { Toast.makeText(getApplicationContext(), "error occured during opening camera", Toast.LENGTH_SHORT) .show(); } } }
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blogspot.kantapp.cameraexampleinandroid"> <uses-permission android:name="android.permission.CAMERA" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
No comments:
Post a Comment