Here, you can open camera using our app and take pictures and store in your SDCARD...
My Main.class :
package com.example.cam;
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;
int requestcode=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.img);
b=(Button)findViewById(R.id.btn);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
protected void onActivityResult(int requestcode,int resultcode,Intent data)
{
super.onActivityResult(requestcode, resultcode, data);
if(requestcode==0)
{
Bitmap image=(Bitmap) data.getExtras().get("data");
img.setImageBitmap(image);
}
else if(requestcode==RESULT_CANCELED)
{
Toast.makeText(getApplicationContext(), "Camera not working", 3000).show();
}
}
}
Here:
Intent i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
android.provider.MediaStore.ACTION_IMAGE_CAPTURE is a service in which the Camera action is located.Provider.MediaStore is used to set the Media location && Action_Image_Capture is to Take a Snap using camera.
Here is XML Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="capture" />
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
Hi. I’m Designer of TechBite. I’m CEO/Founder of TEchBite. I’m Creative App Developer, Web Designer, UI/UX Designer, Web Developer, Business Enthusiast, StartUp Enthusiast. Inspired to make things looks better.
0 comments:
Post a Comment