Java乔晓松-android中调用系统拍照功能并显示拍照的图片
android中调用系统拍照功能并显示拍照的图片
如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图
代码如下:
package com.example.myphotos;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;/*** 2013-6-27 上午10:27:23* * @author 乔晓松*/
public class CameraActivity extends Activity {private Button button;private ImageView imageView;private String fileName;@SuppressLint({ "SimpleDateFormat", "SdCardPath" })@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.activity_camera);button = (Button) findViewById(R.id.btn_camera);imageView = (ImageView) findViewById(R.id.imageView1);File file = new File("/sdcard/myImage/");file.mkdirs();// 创建文件夹button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, 1);}});// Intent intent = new Intent();// intent.setAction("android.intent.action.MAIN");// intent.addCategory("android.intent.category.LAUNCHER");// intent.setFlags(0x10200000);// intent.setComponent(new ComponentName("com.android.camera",// "com.android.camera.Camera"));// startActivity(intent);}@SuppressLint({ "SdCardPath", "SimpleDateFormat" })@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == Activity.RESULT_OK) {String sdStatus = Environment.getExternalStorageState();if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用Log.v("TestFile","SD card is not avaiable/writeable right now.");return;}SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");String name = format.format(new Date());fileName = "/sdcard/myImage/" + name + ".jpg";Bundle bundle = data.getExtras();Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式FileOutputStream b = null;try {b = new FileOutputStream(fileName);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {b.flush();b.close();} catch (IOException e) {e.printStackTrace();}}imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里}}
}
上面获取到的拍摄照片的缩略图,要想获取到拍摄照片的原图,就要在打开相机的时候,把照片保存的地址必须设置好,代码如下:
package com.example.myphotos;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;/*** 2013-6-27 上午10:27:23* * @author 乔晓松*/
public class CameraActivity extends Activity {private Button button;private ImageView imageView;private String fileName;@SuppressLint({ "SimpleDateFormat", "SdCardPath" })@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.activity_camera);button = (Button) findViewById(R.id.btn_camera);imageView = (ImageView) findViewById(R.id.imageView1);File file = new File("/sdcard/myImage/");file.mkdirs();// 创建文件夹button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");String name = format.format(new Date());fileName = "/sdcard/myImage/" + name + ".jpg";Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(fileName)));startActivityForResult(intent, 1);}});// Intent intent = new Intent();// intent.setAction("android.intent.action.MAIN");// intent.addCategory("android.intent.category.LAUNCHER");// intent.setFlags(0x10200000);// intent.setComponent(new ComponentName("com.android.camera",// "com.android.camera.Camera"));// startActivity(intent);}@SuppressLint({ "SdCardPath", "SimpleDateFormat" })@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == Activity.RESULT_OK) {String sdStatus = Environment.getExternalStorageState();if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用Log.v("TestFile","SD card is not avaiable/writeable right now.");return;}Bundle bundle = data.getExtras();Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式FileOutputStream b = null;try {b = new FileOutputStream(fileName);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {b.flush();b.close();} catch (IOException e) {e.printStackTrace();}}imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里}}
}
布局文件代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><Buttonandroid:id="@+id/btn_camera"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:text="@string/btn_carema" /><ImageViewandroid:id="@+id/imageView1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignLeft="@+id/btn_camera"android:layout_below="@+id/btn_camera"android:layout_marginTop="17dp"android:background="#999999"tools:ignore="ContentDescription" /></RelativeLayout>
现在正在学习阶段,如有错误之处,请大牛们指导,我定修改此文章...
Java乔晓松-android中调用系统拍照功能并显示拍照的图片相关推荐
- java手势识别技术_Java乔晓松-android中的手势识别的详解
对于android中的手势识别,效果如下图所示: 模拟器中有个可以添加手势的工具Gestures Builder,可以添加手势如下图所示: 当添加手势成功后,会在Sdcard上产生保存的手势的文件ge ...
- Android中调用系统已安装的播放器来播放网络流媒体视频
2019独角兽企业重金招聘Python工程师标准>>> 实现思路比较简单几行代码就可以搞定,在界面放一个Button或者带有播放图标的imageview,点击事件中调用本地播放器来播 ...
- oracle 条件查询加排序,Java乔晓松-oracle的条件查询和排序查询
在查询中过滤行 过滤 使用 WHERE 子句,将不满足条件的行过滤掉. SELECT *|{[DISTINCT] column|expression [alias],...} FROM tabl ...
- 安卓手机如何打开php文件夹,Android_Android中调用系统的文件浏览器及自制简单的文件浏览器,调用系统自带的文件浏览器- phpStudy...
Android中调用系统的文件浏览器及自制简单的文件浏览器 调用系统自带的文件浏览器这很简单: /** 调用文件选择软件来选择文件 **/ private void showFileChooser() ...
- Android开发中调用系统窗口的方法
//直接拨号 Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:12345678")); start ...
- Android多层嵌套Fragment中调用系统相机拍照以及裁剪的相关问题解决
多层嵌套Fragment中调用系统相机拍照.裁剪 最近公司项目有频繁使用相机拍照的需求,然后频繁的实际使用过程中遇到很多大大小小的坑,在此记录下. (ps.关于一些拍照后图片旋转.裁剪设置返回数据为t ...
- 在 Android 中调用二进制可执行程序(native executable )
在 Android 中调用二进制可执行程序(native executable ) 前几天有需要在java代码中调用二进制程序,就在网上找了些资料,写点东西记录下. Android 也是基于linux ...
- 随笔记录2、Android端调用系统分享文件记录
Android端调用系统发送文件 效果如如下: Android 7.0以上需要适配 第一步: 1. AndroidManifest.xml中注册.provider 2. android:resourc ...
- Android中背光系统架构
Mg Android中背光系统架构 Email:wei7758@126.com Blog:http://blog.csdn.net/yinwei520 Author:Yww Time:2011-9-2 ...
最新文章
- 不是不去爱,爱了也是一种伤害!
- ue4场景没阴影_UE4 Mobile使用动态阴影的一些小结
- 机器视觉资料汇总(2)
- log4j按天生成且有按照文件大小分割_论文阅读TGM:轨迹生成
- 毕业季,拿走你急需的求职面试技巧
- Jfreechart柱形图设置数值显示
- [poj3692]Kindergarten
- 为什么LED灯会越用越暗?
- JAVA Metrics 度量工具使用介绍1
- vivado和modelsim联合仿真实现分频器——2的n次方分频
- python切片为列表增加元素_python – 使用切片语法来加入列表的一部分列表元素...
- 计算机应用基础——计算机软件(一)
- C语言发展史的点点滴滴
- ODL中的Karaf命令使用
- [android]so easy实现根据viewpager、edittext、时间使背景动态渐变色效果~
- Mac OS X: 苹果机支持的分区表类型及问题解决
- 德艺双馨艺术家——刘文军
- 原来华为手机还有3大隐藏省电功能,开启后,两天一充很简单
- html 水印插件,Watermark.js 在浏览器端添加水印插件 - 文章教程
- 企业管理之道:中层管理者,到底该管理什么?
热门文章
- 【MySQL】数据库事务处理---MySQL
- 技巧 | 栅格的属性数据和经纬度是分开的两个文件,怎么将它们整合到同一个文件上...
- mysql命令导出数据不完整_使用MySQL MySqldump命令导出数据时的注意事项
- 前端工程师如何进行职业规划?
- 接口不能被实例化的吗?接口引用是什么?
- python迭代器是什么意思_python 迭代器(第二次总结)
- Linux搭建虚拟专用,Ubuntu的发行版如何搭建虚拟专用网
- c语言怎样计算栈的长度,请问,用c语言做一个计算器 包括+-*/()的运算 用栈 该怎么做...
- Nginx SSI指令配置详解
- java socket长连接怎么维持_socket长连接的维持