Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the code which will capture the photo and will show in image view</p> <pre><code>package com.android.test; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MyCameraActivity extends Activity { private static final int CAMERA_REQUEST = 1888; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.imageView = (ImageView)this.findViewById(R.id.imageView1); Button photoButton = (Button) this.findViewById(R.id.button1); photoButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST &amp;&amp; resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); } } } </code></pre> <p>Here is the XML resource:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"&gt;&lt;/Button&gt; &lt;ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"&gt;&lt;/ImageView&gt; &lt;/LinearLayout&gt; </code></pre> <p>Don't forget to add the permission:</p> <pre><code> &lt;uses-feature android:name="android.hardware.camera"&gt;&lt;/uses-feature&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.CAMERA" /&gt; </code></pre> <p>If you received <strong>data as null in activity result</strong> then use following code:</p> <p>Code to get high resolution images from camera.</p> <pre><code>Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); /* Here destination is the File object in which your captured images will be stored */ intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination)); /* Here REQUEST_IMAGE is the unique integer value you can pass it any integer */ startActivityForResult(intent, REQUEST_IMAGE); </code></pre> <p>After this implement the onActivityResult method like below</p> <pre><code>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == REQUEST_IMAGE &amp;&amp; resultCode == Activity.RESULT_OK) { // Now check the file which you have pass with the intent to capture. // Camera had stored the captured image to the file which you have passed with the intent. } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload