dcyde

Untitled

Jun 19th, 2017
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5.  
  6. import android.content.Context;
  7. import android.hardware.Camera;
  8. import android.hardware.Camera.PictureCallback;
  9. import android.os.Environment;
  10. import android.util.Log;
  11. import android.widget.Toast;
  12.  
  13. public class PicClick implements PictureCallback {
  14.    
  15.     private final Context context;
  16.  
  17.     public PicClick(Context context) {
  18.         this.context = context;
  19.     }
  20.  
  21.     @Override
  22.     public void onPictureTaken(byte[] data, Camera camera) {
  23.        
  24.         File pictureFileDir = getDir();
  25.  
  26.         if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
  27.  
  28.             Log.d("Can't create directory to save image.", null);
  29.             Toast.makeText(context, "Can't create directory to save image.",
  30.                     Toast.LENGTH_LONG).show();
  31.             return;
  32.  
  33.         }
  34.  
  35.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
  36.         String date = dateFormat.format(new Date());
  37.         String photoFile = "Picture_" + date + ".jpg";
  38.  
  39.         String filename = pictureFileDir.getPath() + File.separator + photoFile;
  40.  
  41.         File pictureFile = new File(filename);
  42.  
  43.         try {
  44.             FileOutputStream fos = new FileOutputStream(pictureFile);
  45.             fos.write(data);
  46.             fos.close();
  47.             Toast.makeText(context, "New Image saved:" + photoFile,
  48.                     Toast.LENGTH_LONG).show();
  49.         } catch (Exception error) {
  50.             Log.d("File" + filename + "not saved: "
  51.                     + error.getMessage(), null);
  52.             Toast.makeText(context, "Image could not be saved.",
  53.                     Toast.LENGTH_LONG).show();
  54.         }
  55.     }
  56.  
  57.     private File getDir() {
  58.         File sdDir = Environment
  59.           .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  60.         return new File(sdDir, "CameraAPIDemo");
  61.     }
  62. }
Add Comment
Please, Sign In to add comment