Android Camera, Atlas Demo_android

Source: Internet
Author: User

This article for you to share the Android camera, Atlas Basic demo, for your reference, the specific content as follows

Package Com.example.democamera;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import android.app.Activity;
Import Android.content.Context;
Import android.content.Intent;
Import android.content.res.Configuration;
Import Android.database.Cursor;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.view.View;
 
Import Android.widget.ImageView;
  public class Mainactivity extends activity {private ImageView IV;
 
  static final int gallery = 1, camera = 2;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 
    Setcontentview (R.layout.activity_main);
  IV = (ImageView) Findviewbyid (r.id.iv_picture); /** * Start Picture Gallery * * @param view */public void startgallery (view view) {Intent Intent = new Intent (
    ); Intent.setAction (intent.action_get_content);
    Intent.settype ("image/*");
 
  Startactivityforresult (Intent, gallery); /** * Boot Camera * * @param view/public void Startcamera (view view) {Intent Intent = new Intent (Med
 
    Iastore.action_image_capture); if (Issdexsit ()) {/* Create a store Picture folder */File dir = new file (environment.getexternalstoragedirectory () + "
      /my ");
      if (!dir.exists ()) dir.mkdirs ();
      /* Set picture parameters to get the original size of the picture * * File = new file (dir, "aaa.jpg");
      Intent.putextra (Mediastore.extra_screen_orientation, configuration.orientation_undefined);
    Intent.putextra (Mediastore.extra_output, Uri.fromfile (file));
 
  } startactivityforresult (Intent, camera); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO Auto-generat
 
    Ed method Stub Super.onactivityresult (Requestcode, ResultCode, data);
    Switch (requestcode) {Case GALLERY:  /* Make a judgment to prevent return error * */if (data!= null) {/* Three ways to process URI/URI uri = Data.getdata ();
 
        Iv.setimageuri (URI);
        Bitmap Bitmap = bitmapfactory. DecodeFile (Getpathbyuri (URI, this));
 
        Iv.setimagebitmap (bitmap);
        Bitmap bitmap2 = Getbitmapbyuri (URI, this);
      Iv.setimagebitmap (BITMAP2);
 
    } break;
        Case CAMERA:IF (data!= null) {//This is the method for obtaining thumbnails Bitmap B = (Bitmap) Data.getextras (). Get ("data");
 
      Iv.setimagebitmap (b);
        else {//has SD card to get picture original Bitmap Bitmap = Bitmapfactory.decodefile ("sdcard/my/aaa.jpg");//Direct This will have the risk of Oom, demo simple so deal with
      Iv.setimagebitmap (bitmap);
    } break;
    Default:break; }/** * Uri-->bitmap * * @param Uri * @param context * @return/public static Bitmap G
    Etbitmapbyuri (URI URI, context context) {Bitmap Bitmap = null; try {bitmap = MediaStore.Images.Media.getBitmaP (Context.getcontentresolver (), URI);
    catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
    catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
  return bitmap; /** * URI--->path * * @param URI * @param context * @return/public static String GETPATHB
 
    YUri (URI URI, context context) {String path = null;
    string[] projection = {MediaStore.Images.Media.DATA};
    Cursor Cursor = Context.getcontentresolver (). Query (URI, projection, NULL, NULL, NULL);
      if (Cursor.movetofirst ()) {int index = cursor. Getcolumnindexorthrow (MediaStore.Images.Media.DATA);
    Path = cursor.getstring (index);
 
  return path; /** * To determine if the SD card exists * * @return/public static Boolean issdexsit () {if Environment.getexternalsto Ragestate (). Equals (environment.media_mounted)) {RetuRN true;
    else {return false; }
  }
 
}

Share the implementation code for specific Android calling cameras, open albums, and crop pictures below

Private ImageView Iv_user_photo;
  Private String FileName = "";
  Private File tempfile;
  private int crop = 300;//cropping size private static final int open_camera_code = 10;
  private static final int open_gallery_code = 11;


  private static final int crop_photo_code = 12;
      Private Onclicklistener Popupwindowitemonclick = new Onclicklistener () {@Override public void OnClick (View v) {
      Menuwindow.dismiss ();
        Switch (V.getid ()) {//Photo case r.id.btn_camera:initfile ();
        Opencamera ();
      Break
        Photo Album case R.id.btn_gallery:initfile ();
        Opengallery ();
      Break
      Default:break;

  }
    }
  }; public void Initfile () {if (Filename.equals (")) {if (Fileutil.existsdcard ()) {String Path = Environmen
        T.getexternalstoragedirectory () + File.separator + "janubookingonline" + file.separator;
        Fileutil.mkdir (path);
        LOGGER.I ("Path:" + path); FileName = PatH + "user_head_photo.jpg";
      Tempfile = new File (fileName);
      else {commonuitl.toast (context, "Please insert SD card"); /** * Call camera/public void Opencamera () {Intent Intent = new Intent (mediastore.action_image_cap
    ture)//Open camera Intent.putextra ("Output", Uri.fromfile (Tempfile));
  Startactivityforresult (Intent, Open_camera_code); /** * Open album */public void Opengallery () {Intent Intent = new Intent (intent.action_pick);//Open album Inte
    Nt.setdataandtype (MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
    Intent.putextra ("Output", Uri.fromfile (Tempfile));
  Startactivityforresult (Intent, Open_gallery_code); /** * Crop picture * @param uri/public void Cropphoto (Uri uri) {Intent Intent = new Intent ("com.android.ca
    Mera.action.CROP ");
    Intent.setdataandtype (URI, "image/*");
    Intent.putextra ("Output", Uri.fromfile (Tempfile));
    Intent.putextra ("Crop", true);
 Intent.putextra ("Aspectx", 1);   Intent.putextra ("Aspecty", 1);
    Intent.putextra ("Outputx", crop);
    Intent.putextra ("Outputy", crop);
  Startactivityforresult (Intent, Crop_photo_code);
      @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = 1)

    Return
      Switch (requestcode) {case Open_camera_code:cropphoto (Uri.fromfile (tempfile));
    Break
      Case Open_gallery_code:cropphoto (Data.getdata ());
    Break
        Case Crop_photo_code:try {bitmapfactory.options Options = new Bitmapfactory.options ();
        Options.insamplesize = 2;

        Bitmap Bitmap = Bitmapfactory.decodefile (fileName, Options);
          if (bitmap!= null) {Iv_user_photo.setimagebitmap (bitmap);
        Commonuitl.sharedpreferences (context, Appconstants.user_photo, fileName);
      } catch (Exception e) {e.printstacktrace ();
    } break;
    Default:break; } super.onactivityResult (Requestcode, ResultCode, data);

 }

The above is about the Android camera, Atlas of the basic operating content, I hope to learn more about Android software programming help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.