Android Open camera and photo album instance code _android

Source: Internet
Author: User

This example for you to share the Android open camera and photo album specific code for your reference, the specific contents are as follows

Turn on the camera

 /**
   * Select camera

  /private void Showcamera () {
    //Jump to System camera
    Intent cameraintent = new Intent ( Mediastore.action_image_capture);
    if (Cameraintent.resolveactivity (Getpackagemanager ())!= null) {
      //Set the output path after the system camera is photographed
      //create temporary files
      mtmpfile = Otherutils.createfile (Getapplicationcontext ());

      Cameraintent.putextra (Mediastore.extra_output, Uri.fromfile (Mtmpfile));
      Startactivityforresult (Cameraintent, Request_camera);
    } else {
      Toast.maketext (Getapplicationcontext (), R.string.msg_no_camera, Toast.length_short). Show ();

   

Open album

Intent albumintent = new Intent (Intent.action_pick, null);
    Albumintent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
    Startactivityforresult (Albumintent, REQUEST_ALBUM_OK); 

Onactivityresult

 @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (R

    Equestcode, ResultCode, data); Switch (requestcode) {case Request_camera://Request Camera if (Mtmpfile!= null) {LOG.D (TAG, "OnA

          Ctivityresult: Request Camera: "+ mtmpfile.getabsolutepath ());
              Picasso.with (this). Load (mtmpfile). Centercrop (). Resize (otherutils.dip2px (this,100), otherutils.dip2px (this,100))
        . Error (R.MIPMAP.PICTURES_NO). into (Mivdispaly);
      } break;
        Case REQUEST_ALBUM_OK:LOG.D (TAG, "Onactivityresult: Album" + Data.getdata (). toString ());

        Contentresolver resolver = Getcontentresolver ();

          try {inputstream InputStream = Resolver.openinputstream (Data.getdata ());

          Bitmap Bitmap = Bitmapfactory.decodestream (InputStream);

        Mivdispaly.setimagebitmap (bitmap);
        catch (FileNotFoundException e) {e.printstacktrace ();

    }    Cursor query = Resolver.query (Data.getdata (), NULL, NULL, NULL, NULL);
String str = NULL;
while (Query.movetonext ()) {//LOG.D (TAG, "Onactivityresult: Quantity" + Query.getcount ());
Str =query.getstring (Query.getcolumnindex (MediaStore.Images.Media.DATA));
LOG.D (TAG, "onactivityresult: Column name" + query.getstring (Query.getcolumnindex (MediaStore.Images.Media.DATA)));
}//Query.close (); Picasso.with (this). Load (new File (str)). Centercrop (). Resize (otherutils.dip2px (this,100), otherutils.dip2px (this
        )//. Error (R.MIPMAP.PICTURES_NO). into (Mivdispaly);

    Break 

 }
  }

Otherutils 

Import Android.content.Context;

Import android.os.Environment;
Import Java.io.File;

Import Java.util.Date; /** * Created by Liu Nan on 2016/7/20 0020.22:37/public class Otherutils {public static int dip2px
    At Dpvalue) {Final float scale = context.getresources (). Getdisplaymetrics (). density;
  return (int) (Dpvalue * scale + 0.5f); public static int Px2dip (context context, float Pxvalue) {final float scale = context.getresources (). Getdisplaym
    Etrics (). density;
  return (int) (Pxvalue/scale + 0.5f);  /** * Get photo photo storage File * @param context * @return/public static file CreateFile {file
    File if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {String TimeStamp = string.valueof (new
      Date (). GetTime ());
    File = new file (environment.getexternalstoragedirectory () + File.separator + timestamp+ ". jpg");
      }else{File Cachedir = Context.getcachedir (); STring TimeStamp = string.valueof (new Date (). GetTime ());
    File = new file (Cachedir, timestamp+ ". jpg");
  } return file;

 }

}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.