android多媒体——5步学会使用系统相机
2015-09-18 19:27
399 查看
使用系统相机拍照并返回照片步骤如下
【1】权限
【2】通过intent启动相机
【3】设置图片保存位置及名称
【4】启动相机
【5】得到相片
将相片设置在ImageView上面
【1】权限
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
【2】通过intent启动相机
Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
【3】设置图片保存位置及名称
file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");//新建相机图片保存位置,图片名称 try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } ////告知相机图片保存位置 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
【4】启动相机
startActivityForResult(intent, 0x23);
【5】得到相片
将相片设置在ImageView上面
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //data包含缩略图 if (requestCode == 0x23) { if (resultCode == RESULT_OK) { imageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath())); //imageView.setImageURI(Uri.fromFile(file));//得到图片 } } }
程序源码
public class MainActivity extends Activity {
private Button button;
private ImageView imageView;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");//新建相机图片保存位置,图片名称 try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } ////告知相机图片保存位置 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 0x23);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//data缩略图
if (requestCode == 0x23) {
if (resultCode == RESULT_OK) {
imageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
//imageView.setImageURI(Uri.fromFile(file));//得到图片
data.getIntExtra(MediaStore.EXTRA_OUTPUT,0);
}
}
}
}
相关文章推荐
- Android-sqlite3 not found解决
- Android EditText监听只能输入2位小数
- android开发(45) 自定义软键盘(输入法)
- android github 资源地址
- android studio下使用getclassloader().getresourceasstream()的问题
- 【Android】课程表布局实现(支持动态添加课程)
- android开发(44) 使用了 SoundPool 播放提示音
- Android 手机相册图片选择
- android 文件夹
- Android之实现妙趣横生的粘连布局
- android 滑动分页
- android ant 打包报错: [aapt] invalid resource directory name:
- Android四舍五入保留两位小数
- android小游戏“24点”放源码了!!
- Android高手进阶:Adapter深入理解与优化
- Android 流式布局
- Android mediaplayer Couldn't open file on client side, trying server side
- android - AsyncTask 异步操作
- Android M 新的运行时权限开发者需要知道的一切
- (调用百度地图)连续的进入地图然后又退出地图,抛出一个很奇怪的空指针