Android Camera Example Code - Android

How to create android api for existing camera application?

Snippet Code


  
Rate this page :
  [ 0 votes]

The sample code given below allows you to create an android api for the existing camera application installed on your mobile phone.
Here, you are using the Intent object which is used to invoke the camera through Mediastore.ACTION_IMAGE_CAPTURE action. On the same time the image is saved in external storage as MyPhoto.jpg. Atlast call the startActivityForResult() method which is used to trigger the intent object.

MainActivity.java : -------------------- package androidinterview.com.androidcamera; import java.io.File; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { static int TAKE_PIC =1; Uri outPutfileUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void CameraClick(View v) { Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg"); outPutfileUri = Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri); startActivityForResult(intent, TAKE_PIC); } @Override protected void onActivityResult(int requestCode, int resultCode,Intent data) { if (requestCode == TAKE_PIC ampampresultCode==RESULT_OK){ Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show(); } } }

Tags


Ask Questions

Ask Question