Get pictures and crop them from albums and photos separately

Source: Internet
Author: User

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:tools= "Http://schemas.android.com/tools"
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
tools:context= ". Cameraactivity ">

<imageview
android:id= "@+id/imageview"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "center"
android:adjustviewbounds= "true"
android:minheight= "150dip"
android:minwidth= "150dip"/>

<button
android:id= "@+id/btnphone"
android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "album"/>

<button
android:id= "@+id/btntakepicture"
android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Photo"/>

</LinearLayout>

Import Java.io.ByteArrayOutputStream;
Import Java.io.File;

Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import android.app.Activity;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.ImageView;

public class Cameraactivity extends Activity {
private static final int NONE = 0;
private static final int photo_graph = 1;//photo
private static final int photo_zoom = 2; Scaling
private static final int photo_resoult = 3;//result
private static final String image_unspecified = "image/*";
Private ImageView ImageView = null;
Private Button btnphone = null;
Private Button btntakepicture = null;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_camera);

ImageView = (ImageView) Findviewbyid (R.id.imageview);
Btnphone = (Button) Findviewbyid (R.id.btnphone);
Btnphone.setonclicklistener (Onclicklistener);
Btntakepicture = (Button) Findviewbyid (r.id.btntakepicture);
Btntakepicture.setonclicklistener (Onclicklistener);
}

Private final View.onclicklistener Onclicklistener = new View.onclicklistener () {

@Override
public void OnClick (View v) {
if (v = = Btnphone) {//Get pictures from albums
Intent Intent = new Intent (Intent.action_pick, NULL);
Intent.setdataandtype (
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
image_unspecified);
Startactivityforresult (Intent, photo_zoom);
} else if (v = = btntakepicture) {//Get picture from photo
Intent Intent = new Intent (mediastore.action_image_capture);
Intent.putextra (Mediastore.extra_output, Uri.fromfile (new File) (
Environment.getexternalstoragedirectory (), "temp.jpg"));
Startactivityforresult (Intent, photo_graph);
}

}

};
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (ResultCode = = NONE)
Return
Photo
if (Requestcode = = photo_graph) {
Set File Save path
File picture = new file (Environment.getexternalstoragedirectory ()
+ "/temp.jpg");
Startphotozoom (Uri.fromfile (picture));
}
if (data = = NULL)
Return
Read album Zoom picture
if (Requestcode = = Photo_zoom) {
Startphotozoom (Data.getdata ());
}
Processing results
if (Requestcode = = Photo_resoult) {
Bundle extras = Data.getextras ();
if (extras! = null) {
Bitmap photo = extras.getparcelable ("Data");
Bytearrayoutputstream stream = new Bytearrayoutputstream ();
Photo.compress (Bitmap.CompressFormat.JPEG, (), stream);//(0-100) Compress files
The bitmap can be saved to the SD card here.
Imageview.setimagebitmap (photo); Display the picture on the ImageView control
}

}
Super.onactivityresult (Requestcode, ResultCode, data);
}

/**
* Shrink Picture
*
* @param URI
*/
public void Startphotozoom (Uri uri) {
Intent Intent = new Intent ("Com.android.camera.action.CROP");//Call a picture clipping page that comes with the Android system,
Intent.setdataandtype (URI, image_unspecified);
Intent.putextra ("Crop", "true");//Trimming
Aspectx Aspecty is the ratio of width to height
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Outputx Outputy is the cropped picture width high
Intent.putextra ("Outputx", 64);
Intent.putextra ("Outputy", 64);
Intent.putextra ("Return-data", true);
Startactivityforresult (Intent, Photo_resoult);
}
}

Get pictures and crop them from albums and photos separately

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.