android编程实现系统图片剪裁的方法
程序员文章站
2023-12-12 18:19:22
本文实例讲述了android编程实现系统图片剪裁的方法。分享给大家供大家参考,具体如下:
package cn.test;
import java.io.fil...
本文实例讲述了android编程实现系统图片剪裁的方法。分享给大家供大家参考,具体如下:
package cn.test; import java.io.file; import java.text.simpledateformat; import java.util.date; import android.app.activity; import android.content.contentresolver; import android.content.contenturis; import android.content.intent; import android.database.cursor; import android.graphics.bitmap; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; public class cutactivity extends activity { private button button; private imageview imageview; private file mcurrentphotofile; private bitmap camerabitmap; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); button = (button) findviewbyid(r.id.button); imageview = (imageview) findviewbyid(r.id.imageview); button.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent intent = new intent( "android.media.action.image_capture"); mcurrentphotofile = new file( "mnt/sdcard/dcim/camera/", getphotofilename()); intent.putextra(mediastore.extra_output, uri.fromfile(mcurrentphotofile)); startactivityforresult( intent, activity.default_keys_dialer); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { case 1: uri imguri = mediastore.images.media.external_content_uri; contentresolver cr = cutactivity.this .getcontentresolver(); uri fileuri = uri.fromfile(mcurrentphotofile); sendbroadcast(new intent(intent.action_media_scanner_scan_file, fileuri)); try { thread.sleep(3000); } catch (interruptedexception e) { e.printstacktrace(); } cursor cursor = cr .query(imguri, null, mediastore.images.media.display_name + "='" + mcurrentphotofile.getname() + "'", null, null); uri uri = null; if (cursor != null && cursor.getcount() > 0) { cursor.movetolast(); long id = cursor.getlong(0); uri = contenturis.withappendedid(imguri, id); } final intent intent = new intent( "com.android.camera.action.crop"); intent.setdataandtype(uri, "image/*"); intent.putextra("crop", "true"); intent.putextra("outputx", 380); intent.putextra("outputy", 500); intent.putextra("return-data", true); cutactivity.this.startactivityforresult(intent, 3); break; case 2: break; case 3: if (data != null) { camerabitmap = (bitmap) data.getextras().get("data"); imageview.setimagebitmap(camerabitmap); } break; default: break; } super.onactivityresult(requestcode, resultcode, data); } private string getphotofilename() { date date = new date(system.currenttimemillis()); simpledateformat dateformat = new simpledateformat( "'img'_yyyymmdd_hhmmss"); return dateformat.format(date) + ".jpg"; } }
希望本文所述对大家android程序设计有所帮助。