Android calls system cameras and albums

Source: Internet
Author: User

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

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.