在android中,照相功能系统已经提供,在app中可以直接使用。当手机从android play里面下载有照相功能的应用时, 会判断手机是否支持。不支持,不给予下载。
照相有几个步骤:
1. 声明权限
2. 使用camera照相
3. 显示图片
<uses-feature android:name="android.hardware.camera" />
2. 使用camera照相
private void dispatchtakepictureintent(int actioncode) {
intent takepictureintent = new intent(mediastore.action_image_capture);
startactivityforresult(takepictureintent, actioncode);
}
3. 显示图片
<pre class=java name="code">@override
protected void onactivityresult(int requestcode, int resultcode, intent data) {
switch (requestcode) {
bundle extras = intent.getextras();
bitmap mimagebitmap = (bitmap) extras.get("data");
mimageview.setimagebitmap(mimagebitmap);
}</pre>
<pre></pre>
<pre></pre>
intent takepictureintent = new intent(mediastore.action_image_capture);
file f = null;
try {
f = setupphotofile();
takepictureintent.putextra(mediastore.extra_output, uri.fromfile(f));
} catch (ioexception e) {
e.printstacktrace();
f = null;
}
private file createimagefile() throws ioexception {
// create an image file name
string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());
string imagefilename = "img_"+ timestamp + "_";
file albumf = getalbumdir();
file imagef = file.createtempfile(imagefilename, "jpg", albumf);
return imagef;
}
private file setupphotofile() throws ioexception {
file f = createimagefile();
mcurrentphotopath = f.getabsolutepath();
return f;
}
private file getalbumdir() {
file storagedir = null;
if (environment.media_mounted.equals(environment.getexternalstoragestate())) {
storagedir = malbumstoragedirfactory.getalbumstoragedir(getalbumname());
if (storagedir != null) {
if (! storagedir.mkdirs()) {
if (! storagedir.exists()){
log.d("camerasample", "failed to create directory");
return null;
}
}
}
} else {
log.v(getstring(r.string.app_name), "external storage is not mounted read/write.");
}
return storagedir;
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
网友评论