How Android gets photos from your system or camera

Source: Internet
Author: User

public class Mainactivity extends Activity

{


private static final int photo_request_carema = 1;//photo

private static final int photo_request_gallery = 2;//Select from album

private static final int photo_request_cut = 3;//result


Private ImageView iv_image;


/* Avatar Name */

private static final String Photo_file_name = "Temp_photo.jpg";

Private File tempfile;


@Override

protected void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

This.iv_image = (ImageView) This.findviewbyid (r.id.iv_image);

}


/*

* Get from albums

*/

public void Gallery (view view)

{

Activate the System gallery, select a picture

Intent Intent = new Intent (Intent.action_pick);

Intent.settype ("image/*");

Open an activity with a return value, the request code is Photo_request_gallery

Startactivityforresult (Intent, photo_request_gallery);

}


/*

* Get from Camera

*/

public void camera (view view)

{

Activating the camera

Intent Intent = new Intent ("Android.media.action.IMAGE_CAPTURE");

Determine if the memory card is usable and can be stored

if (Hassdcard ())

{

Tempfile = new File (Environment.getexternalstoragedirectory (),

Photo_file_name);

To create a URI from a file

Uri uri = uri.fromfile (tempfile);

Intent.putextra (Mediastore.extra_output, URI);

}

Open an activity with a return value, the request code is Photo_request_carema

Startactivityforresult (Intent, Photo_request_carema);

}


/*

* Cut Pictures

*/

private void crop (URI uri)

{

Crop image Intent

Intent Intent = new Intent ("Com.android.camera.action.CROP");

Intent.setdataandtype (URI, "image/*");

Intent.putextra ("Crop", "true");

The scale of the cropping box, 1:1

Intent.putextra ("Aspectx", 1);

Intent.putextra ("Aspecty", 1);

Size of output picture after cropping

Intent.putextra ("Outputx", 250);

Intent.putextra ("Outputy", 250);


Intent.putextra ("OutputFormat", "JPEG");//Picture format

Intent.putextra ("Nofacedetection", true);//Remove Face recognition

Intent.putextra ("Return-data", true);

Open an activity with a return value, the request code is Photo_request_cut

Startactivityforresult (Intent, photo_request_cut);

}


/*

* Determine if sdcard is being mounted

*/

Private Boolean Hassdcard ()

{

if (Environment.getexternalstoragestate (). Equals (

environment.media_mounted))

{

return true;

} else

{

return false;

}

}


@Override

protected void Onactivityresult (int requestcode, int resultcode, Intent data)

{

if (Requestcode = = photo_request_gallery)

{

Data returned from the album

if (data! = NULL)

{

Get the full path of the picture

Uri uri = Data.getdata ();

Crop (URI);

}


} else if (Requestcode = = Photo_request_carema)

{

Data returned from the camera

if (Hassdcard ())

{

Crop (Uri.fromfile (tempfile));

} else

{

Toast.maketext (Mainactivity.this, "No memory card found, no photos saved! ", 0). Show ();

}


} else if (Requestcode = = photo_request_cut)

{

Data returned from the cut image

if (data! = NULL)

{

Bitmap Bitmap = Data.getparcelableextra ("Data");

This.iv_image.setImageBitmap (bitmap);

}

Try

{

Delete a temporary file

Tempfile.delete ();

} catch (Exception e)

{

E.printstacktrace ();

}


}


Super.onactivityresult (Requestcode, ResultCode, data);

}

}


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.