Home>

When we need to upload pictures,Want to crop to the size we need,Android phones come with this feature,It ’s very easy, so how does this function work?Here we introduce to you Android development to get pictures cropped from camera or album,

stop talking nonsense,Post the code directly to everyone.

package com.only.android.app;
import java.io.file;
import android.app.activity;
import android.app.alertdialog;
import android.content.dialoginterface;
import android.content.intent;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.net.uri;
import android.os.bundle;
import android.os.systemclock;
import android.provider.mediastore;
import android.view.view;
import android.widget.button;
import android.widget.imageview;
import com.only.android.r;
public class copyofimagescaleactivity extends activity implements view.onclicklistener {
 /** called when the activity is first created. * /
 private button selectimagebtn;
 private imageview imageview;
 private file sdcardtempfile;
 private alertdialog dialog;
 private int crop=180;
 @override
 public void oncreate (bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  setcontentview (r.layout.imagescale);
  selectimagebtn=(button) findviewbyid (r.id.selectimagebtn);
  imageview=(imageview) findviewbyid (r.id.imageview);
  selectimagebtn.setonclicklistener (this);
  sdcardtempfile=new file ("/mnt/sdcard /", "tmp_pic_" + systemclock.currentthreadtimemillis () + ".jpg");
 }
 @override
 public void onclick (view v) {
  if (v == selectimagebtn) {
   if (dialog == null) {
    dialog=new alertdialog.builder (this) .setitems (new string [] {"camera", "albums"}, new dialoginterface.onclicklistener () {
     @override
     public void onclick (dialoginterface dialog, int which) {
      if (which == 0) {
       intent intent=new intent ("android.media.action.image_capture");
       intent.putextra ("output", uri.fromfile (sdcardtempfile));
       intent.putextra ("crop", "true");
       intent.putextra ("aspectx", 1);//The cropping box proportion
       intent.putextra ("aspecty", 1);
       intent.putextra ("outputx", crop);//output image size
       intent.putextra ("outputy", crop);
       startactivityforresult (intent, 101);
      } else {
       intent intent=new intent ("android.intent.action.pick");
       intent.setdataandtype (mediastore.images.media.internal_content_uri, "image/*");
       intent.putextra ("output", uri.fromfile (sdcardtempfile));
       intent.putextra ("crop", "true");
       intent.putextra ("aspectx", 1);//The cropping box proportion
       intent.putextra ("aspecty", 1);
       intent.putextra ("outputx", crop);//output image size
       intent.putextra ("outputy", crop);
       startactivityforresult (intent, 100);
      }
     }
    }). create ();
   }
   if (! dialog.isshowing ()) {
    dialog.show ();
   }
  }
 }
 @override
 protected void onactivityresult (int requestcode, int resultcode, intent intent) {
  if (resultcode == result_ok) {
   bitmap bmp=bitmapfactory.decodefile (sdcardtempfile.getabsolutepath ());
   imageview.setimagebitmap (bmp);
  }
 }
}
  • Previous Method for customizing android shutdown interface of android source code exploration
  • Next Method for building graphical interface VNC by Centos