Hi, Here I'm going to discuss about how to take picture with camera in android and set that picture to ImageView.
So first create one activity, I have given name as CameraActivity.java and the layout file for it is named as activity_camera.xml
Permission:
For using camera and to store picture on storage we need define permissions under manifest.xml
So first create one activity, I have given name as CameraActivity.java and the layout file for it is named as activity_camera.xml
Permission:
For using camera and to store picture on storage we need define permissions under manifest.xml
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
activity_camera.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/activity_camera" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:src="@mipmap/ic_launcher"/> <Button android:id="@+id/btuttonTakePic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="100dp" android:text="Take Picture"/> </RelativeLayout>CameraActivity.javapublic class CameraActivity extends AppCompatActivity { ImageView imageView; Button buttonTakePicture; private final int CAMERA_PIC_REQUEST = 1001; private Uri outputUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); imageView = (ImageView) findViewById(R.id.imageView); buttonTakePicture = (Button) findViewById(R.id.btuttonTakePic); buttonTakePicture.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Long tsLong = System.currentTimeMillis() / 1000; final String timeStamp = tsLong.toString(); Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(createImageFile(), timeStamp + "mypic.png"); outputUri = Uri.fromFile(file); camera_intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputUri); camera_intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); startActivityForResult(camera_intent, CAMERA_PIC_REQUEST); } }); } public File createImageFile() { String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/photos/"; File dir = new File(fullPath); if (!dir.exists()) { dir.mkdirs(); } return dir; } // To resize bitmap to avoid OutOfMemoryException public static Bitmap resizeImageWithMax(Bitmap bitmap, Integer maxDimension) { Bitmap resizedBitmap = null; int originalWidth = bitmap.getWidth(); int originalHeight = bitmap.getHeight(); int newWidth = -1; int newHeight = -1; float multFactor = -1.0F; if (originalHeight > originalWidth) { newHeight = maxDimension; multFactor = (float) originalWidth / (float) originalHeight; newWidth = (int) (newHeight * multFactor); } else if (originalWidth > originalHeight) { newWidth = maxDimension; multFactor = (float) originalHeight / (float) originalWidth; newHeight = (int) (newWidth * multFactor); } else if (originalHeight == originalWidth) { newHeight = maxDimension; newWidth = maxDimension; } resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false); return resizedBitmap; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK) { Bitmap bitmap = null; try { bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), outputUri); } catch (IOException e) { e.printStackTrace(); } if (bitmap != null) { bitmap = resizeImageWithMax(bitmap, 640); imageView.setImageBitmap(bitmap); System.gc(); } else { Toast.makeText(this, "Failed to load image from camera", Toast.LENGTH_SHORT).show(); } } } }Enjoy Coding and share your review and problems in comment so that I can help you out with that.
Sign up here with your email
ConversionConversion EmoticonEmoticon