Preparatory work
Permissions
<!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--请求访问使用照相设备--><uses-permission android:name="android.permission.CAMERA" />
Constant
public final static int album_request_code = 1;
public final static int crop_request = 2;
public final static int camera_request_code = 3;
Photo Path
public static String Saved_image_dir_path = Environment.getexternalstoragedirectory (). GetPath () + "/appname/camera/";
String Camerapath;
Camera
Specify camera to take photo save address
String state = Environment.getexternalstoragestate ();
if (State.equals (environment.media_mounted)) {Camerapath = Saved_image_dir_path + system.currenttimemillis () +". png"; Intent Intent =New Intent ();
Specifies the ACTION intent.setaction (mediastore.action_image_capture) to turn on the system camera; String Out_file_path = Saved_image_dir_path; File dir = new file (Out_file_path); if (!dir.exists ()) {dir.mkdirs (); } //Convert the file address to URI format URI uri = uri.fromfile (new File (Camerapath));
//Set the system camera to take photos after the picture file storage address Intent.putextra (Mediastore.extra_output, URI); Startactivityforresult (Intent, camera_request_code);} else {toast.maketext (Getapplicationcontext (), "Please confirm that the SD card is plugged in", Toast.length_long). Show ();
Onactivityresult
Get Camerapath, just do whatever you want.
@Override
public void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_REQUEST_CODE) { LogUtil.d("path=" + cameraPath); } }
Album
new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, ALBUM_REQUEST_CODE);
Onactivityresult
Call the system album and get the absolute address of the picture via the URI.
@OverridePublicvoidOnactivityresult(int Requestcode,
int ResultCode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (ResultCode = ACTIVITY.RESULT_OK) {
if (Requestcode = = Appconstants.album_request_code) { < br> try {
uri uri = Data.getdata (); &NBSP ;
final String absolutepath=
GetAbsolutePath ( Mactivity, URI);
OGUTIL.D ( "path=" + Absolutepath)
&NB Sp } catch (Exception e) { &N Bsp e.printstacktrace ();
} } &nb Sp } }
GetAbsolutePath method
Public StringGetAbsolutePath(Final context Context,
Final URI Uri) {
if (Null = = URI)ReturnNull
Final String scheme = Uri.getscheme (); String data =Null
if (scheme = =NULL) data = Uri.getpath ();
ElseIf(ContentResolver.SCHEME_FILE.equals (SCHEME)) {data = Uri.getpath (); }ElseIf(ContentResolver.SCHEME_CONTENT.equals (SCHEME)) {CURSOR cursor = context.getcontentresolver (). Query (URI,New String[]{mediastore.images.imagecolumns.data},
NullNullNULL);
if (Null! = cursor) {
if (Cursor.movetofirst ()) {
int index = Cursor.getcolumnindex (
MediaStore.Images.ImageColumns.DATA);
if (Index >-1) {data = cursor.getstring (index); }} cursor.close (); }} return data;}
Android calls system cameras and albums