کد های مربوط به layuot
- <LinearLayout xmlns:android="
" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:background="#000000" android:baselineAligned="false" tools:context=".MainActivity">
<LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical">
<ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="77dp">
- <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#009900" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#EEEEEE" />
<TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff6600" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#EEEEEE" />
<TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff00cc" android:text="" android:textColor="#EEEEEE" android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:textColor="#EEEEEE" android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
<Button android:id="@+id/btnCapturePicture" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginBottom="10dp" android:background="@drawable/gerd" android:text="عکس گرفتن" android:textColor="#EEEEEE" />
<Button android:id="@+id/btnRecordVideo" android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/gerd" android:text="فیلم برداری" android:textColor="#EEEEEE" />
</LinearLayout>
- <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" android:padding="10dp">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="نمایش محتوا" android:padding="10dp" android:textColor="#EEEEEE" android:textSize="15dp" />
<ImageView android:id="@+id/imgPreview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#EEEEEE" android:visibility="gone" />
<VideoView android:id="@+id/videoPreview" android:layout_width="wrap_content" android:layout_height="400dp" android:visibility="gone" />
</LinearLayout>
</LinearLayout>
کد های کلاس جاوا
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
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.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
public class Camera_Test extends Activity {
MediaPlayer mdp,answer,errorr;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
TextView tv1,tv2,tv3,tv4;
// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "AndroidSajad";
private Uri fileUri; // file url to store image/video
private ImageView imgPreview;
private VideoView videoPreview;
private Button btnCapturePicture, btnRecordVideo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_test);
mdp = MediaPlayer.create(Camera_Test.this,R.raw.loading);
answer = MediaPlayer.create(Camera_Test.this,R.raw.answerq);
errorr = MediaPlayer.create(Camera_Test.this,R.raw.errorr);
tv1 = (TextView)findViewById(R.id.textView2);
tv2 = (TextView)findViewById(R.id.textView3);
tv3 = (TextView)findViewById(R.id.textView4);
tv4 = (TextView)findViewById(R.id.textView5);
imgPreview = (ImageView) findViewById(R.id.imgPreview);
videoPreview = (VideoView) findViewById(R.id.videoPreview);
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);
btnCapturePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// capture picture
answer.start();
captureImage();
}
});
btnRecordVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// record video
answer.start();
recordVideo();
}
});
// Checking camera availability
if (!isDeviceSupportCamera()) {
errorr.start();
Toast.makeText(getApplicationContext(),
"دوربین دستگاه شما پشتیبانی نمی شود",
Toast.LENGTH_LONG).show();
finish();
}
}
private boolean isDeviceSupportCamera() {
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/**
* Capturing Camera Image will lauch camera app requrest image capture
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
tv1.setText("فعال کردن دوربین...");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("sajad", fileUri);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("sajad");
}
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
tv2.setText("عکس گرفته شد");
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
tv2.setText("؟؟؟لغو عکس گرفتن");
} else {
// failed to capture image
tv2.setText("!!!اشکال در گرفتن عکس");
}
} else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
tv2.setText("ویدیو با موفقیت ثبت شد");
// video successfully recorded
// preview the recorded video
previewVideo();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
tv2.setText("ضبط فیلم متوقف شد!!!");
} else {
// failed to record video
tv2.setText("خطا در ضبط فیلم...؟؟؟");
}
}
}
/**
* Display image from a path to ImageView
*/
private void previewCapturedImage() {
try {
// hide video preview
videoPreview.setVisibility(View.GONE);
imgPreview.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imgPreview.setImageBitmap(bitmap);
tv3.setText("ذخیره عکس انجام شد");
} catch (NullPointerException e) {
e.printStackTrace();
}
}
/**
* Previewing recorded video
*/
private void previewVideo() {
try {
// hide image preview
imgPreview.setVisibility(View.GONE);
tv3.setText("ذخیره فیلم انجام شد");
videoPreview.setVisibility(View.VISIBLE);
videoPreview.setVideoPath(fileUri.getPath());
// start playing
videoPreview.start();
tv4.setText("نمایش فیلم گرفته شده ");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ------------ Helper Methods ----------------------
* */
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).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;
}
}
فقط مجوز ها رو خودت در منیفست وارد کن"