Android call system camera

Source: Internet
Author: User

Create a syscamera class

package com.recorder.video;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import com.recoder.start.MyRecorderActivity;import com.recoder.start.R;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.util.Log;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class SysCamera extends Activity {public static final int MEDIA_TYPE_IMAGE = 1;public static final int MEDIA_TYPE_VIDEO = 2;private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;private Uri fileUri;Button btvideo,btphoto;@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);            setContentView(R.layout.syscamera);       btphoto=(Button)findViewById(R.id.sysPhoto);    btvideo=(Button)findViewById(R.id.sysVideo);    btphoto.setOnClickListener(new Monitor());    btvideo.setOnClickListener(new Monitor());    }private void PhotoIntent(){ // create Intent to take a picture     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name    // start the image capture Intent    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);}private void VideoIntent(){ //create new Intent    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high    // start the Video Capture Intent    startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);}/** Create a file Uri for saving an image or video */private static Uri getOutputMediaFileUri(int type){      return Uri.fromFile(getOutputMediaFile(type));}/** Create a File for saving an image or video */private static File getOutputMediaFile(int type){if (Environment.getExternalStorageState() == null){return null;}     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(              Environment.DIRECTORY_PICTURES), "MyCameraApp");       // Create the storage directory if it does not exist    if (! mediaStorageDir.exists()){        if (! mediaStorageDir.mkdirs()){            Log.d("MyCameraApp", "failed to create directory");            return null;        }    }    // Create a media file name    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());    File mediaFile;    if (type == MEDIA_TYPE_IMAGE){        mediaFile = new File(mediaStorageDir.getPath() + File.separator +        "IMG_"+ timeStamp + ".jpg");    } else if(type == MEDIA_TYPE_VIDEO) {        mediaFile = new File(mediaStorageDir.getPath() + File.separator +        "VID_"+ timeStamp + ".mp4");    } else {        return null;    }    return mediaFile;} @Override public void onBackPressed() {    Intent in=new Intent(this, MyRecorderActivity.class);    startActivity(in);    super.onBackPressed();    } @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {        if (resultCode == RESULT_OK) {            // Image captured and saved to fileUri specified in the Intent            Toast.makeText(this, "Image saved to:\n" +                     data.getData(), Toast.LENGTH_LONG).show();        } else if (resultCode == RESULT_CANCELED) {            // User cancelled the image capture        } else {            // Image capture failed, advise user        }    }    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {        if (resultCode == RESULT_OK) {            // Video captured and saved to fileUri specified in the Intent            Toast.makeText(this, "Video saved to:\n" +                     data.getData(), Toast.LENGTH_LONG).show();        } else if (resultCode == RESULT_CANCELED) {            // User cancelled the video capture        } else {            // Video capture failed, advise user        }    }}class Monitor implements OnClickListener{@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.sysPhoto:PhotoIntent();break;case R.id.sysVideo:VideoIntent();break;}}}}

Layout file syscamera. xml

<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/sysPhoto"         android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="System Photo"        />    <Button         android:id="@+id/sysVideo"         android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="System Video"        />     </LinearLayout> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.