Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's my application code -- hope it helps: </p> <pre><code>public class CameraActivity extends Activity { ImageView img_logo; public static Bitmap bitmap; public static String selectedImagePath=""; protected boolean _taken; protected static final String PHOTO_TAKEN = "photo_taken"; int count = 0; static String image1 = ""; /*static String _path=""; Bitmap bita; ParsingForCabDetails obj_ParsingForCabDetails; InputStream is; protected boolean _taken; protected static final String PHOTO_TAKEN = "photo_taken";*/ ProgressDialog pd; //ImageView iv; Button cancel,Ok; String alert=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera); img_logo = (ImageView) findViewById(R.id.camera_image); cancel = (Button) findViewById(R.id.camera_taken); upload = (Button) findViewById(R.id.camerabtnupload); if(image1.length()&lt;= 0) startCameraActivity(); upload.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); if(image1.length() &gt; 0){ pd = ProgressDialog.show(SmallyTaxiTabbar.tabContext, "", "Loading...."); new Thread(new Runnable() { public void run() { LoardServer(); handler.sendEmptyMessage(0); } }).start(); } else Toast.makeText(getApplicationContext(),"No Capture Image",2000).show(); } }); img_logo.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub startCameraActivity(); } }); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); selectedImagePath= ""; image1=""; } }); } public void LoardServer() { // this is use for upload image or save image } private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { pd.dismiss(); finish(); if(alert.length() &gt;0 ){ Log.d("hello","alert"+alert); Toast.makeText(getApplicationContext(),alert, 2000).show(); } else FindCabDetails.iv_camera.setImageBitmap(bitmap); selectedImagePath= ""; image1=""; }; }; protected void startCameraActivity() { selectedImagePath = Environment.getExternalStorageDirectory() + "/default"+count+".jpg"; count++; File file = new File(selectedImagePath); Uri outputFileUri = Uri.fromFile(file); Intent intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case -1: onPhotoTaken(); break; } } protected void onPhotoTaken() { _taken = true; bitmap = BitmapFactory.decodeFile(selectedImagePath); bitmap = decodeFile(); image1 = selectedImagePath; img_logo.setImageBitmap(bitmap); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { if (savedInstanceState.getBoolean(PHOTO_TAKEN)) { onPhotoTaken(); } } @Override protected void onSaveInstanceState(Bundle outState) { outState.putBoolean(PHOTO_TAKEN, _taken); } private Bitmap decodeFile() { try { // decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream( selectedImagePath), null, o); final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 &lt; REQUIRED_SIZE || height_tmp / 2 &lt; REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale++; } // decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(new FileInputStream(selectedImagePath), null, o2); } catch (FileNotFoundException e) { } return null; } public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK) { finish(); } return super.onKeyDown(keyCode, event); } } </code></pre> <p><strong>Use this camera.xml</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#FFFFFF" android:layout_height="fill_parent" android:layout_width="fill_parent"&gt; &lt;RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:background="@drawable/bottoombg" android:layout_height="wrap_content"&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:textColor="#FBB917" android:textStyle="bold" android:textSize="20dip" android:id="@+id/txtCabNametitle" android:text="Camera"&gt;&lt;/TextView&gt; &lt;Button android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@drawable/filter_btn1" android:layout_marginRight="5dip" android:layout_height="30dip" android:textColor="#FFFFFF" android:text="Upload" android:layout_width="70dip" android:id="@+id/camerabtnupload"&gt;&lt;/Button&gt; &lt;Button android:layout_centerVertical="true" android:background="@drawable/btn_press_effect" android:layout_height="30dip" android:textColor="#FFFFFF" android:paddingLeft="10dip" android:id="@+id/camera_taken" android:text="Cancel" android:layout_width="70dip"&gt;&lt;/Button&gt; &lt;/RelativeLayout&gt; &lt;ImageView android:id="@+id/camera_image" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_height="200dip" android:layout_width="200dip"&gt;&lt;/ImageView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>AndroidManifest.xml</p> <pre><code> &lt;uses-permission android:name="android.permission.CAMERA" /&gt; &lt;uses-feature android:name="android.hardware.camera" /&gt; &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt; </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