Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try the following code :</p> <pre><code>package com.example.sample1; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.provider.MediaStore; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class CapturePhotoSample1 extends Activity implements OnClickListener { public static final int TAKE_PHOTO=1; ImageView imageView=null; private File folder; String imageFileName=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_capture_photo_sample1); Button button=(Button)this.findViewById(R.id.capture_button); button.setOnClickListener(this); button=null; imageView=(ImageView)this.findViewById(R.id.image_view1); folder = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES)+ "/sample1/"); if (!folder.exists()) { folder.mkdir(); } } private void dispatchTakePictureIntent(int actionCode) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent,actionCode); } @Override public void onClick(View v) { // TODO Auto-generated method stub int id=v.getId(); if(id==R.id.capture_button) this.dispatchTakePictureIntent(TAKE_PHOTO); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu1, menu); return true; } public void handleCameraPhoto(Intent intent) { Bundle extras=intent.getExtras(); Bitmap bitmap=(Bitmap)extras.get("data"); this.imageView.setImageBitmap(bitmap); if(this.isExternalStorageAvailable()) { this.imageFileName="img_"+SDUtil.now(-1)+".png"; /*SDUtil.now() is our own library.It is for creating file name with respect to data and time.IF u copy the hole program means sdutil shows error.For that you write a logic for creating a file name. */ String path=folder+"/"+this.imageFileName; FileOutputStream fos=null; BufferedOutputStream bos=null; try { fos=new FileOutputStream(path); bos=new BufferedOutputStream(fos); bitmap.compress(Bitmap.CompressFormat.PNG, 40, bos); } catch(Exception ex) { ex.printStackTrace(); } if(bos!=null) { try { bos.flush(); //bos.close(); } catch(Exception ex) { ex.printStackTrace(); } } if(bos!=null) { try { bos.close(); } catch(Exception ex) { ex.printStackTrace(); } } if(fos!=null) { try { fos.close(); } catch(Exception ex) { ex.printStackTrace(); } } bos=null; fos=null; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode==Activity.RESULT_OK) { if(requestCode==TAKE_PHOTO) { handleCameraPhoto(data); } } } private boolean isExternalStorageAvailable() { StatFs stat = new StatFs(Environment.getExternalStorageDirectory() .getPath()); double sdAvailSize = (double) stat.getAvailableBlocks() * (double) stat.getBlockSize(); // One binary gigabyte equals 1,073,741,824 bytes. double mbAvailable = sdAvailSize / 1048576; String state = Environment.getExternalStorageState(); boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { // Something else is wrong. It may be one of many other states, but // all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false; } if (mExternalStorageAvailable == true &amp;&amp; mExternalStorageWriteable == true &amp;&amp; mbAvailable &gt; 10) { return true; } else { return false; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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