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

时间:2021-09-09 06:54:08

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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);
  }
 }
}

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