欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发从相机或相册获取图片裁剪

程序员文章站 2023-11-04 14:02:58
废话不多说了,直接给大家贴代码了。 package com.only.android.app; import java.io.file; import a...

Android开发从相机或相册获取图片裁剪

废话不多说了,直接给大家贴代码了。

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[] { "相机", "相册" }, 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);// 裁剪框比例
       intent.putextra("aspecty", 1);
       intent.putextra("outputx", crop);// 输出图片大小
       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);// 裁剪框比例
       intent.putextra("aspecty", 1);
       intent.putextra("outputx", crop);// 输出图片大小
       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);
  }
 }
}

以上代码很简单,相信大家都可以看的懂吧,欲了解更多信息请持续关注本站,谢谢。