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.