博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android调用系统相机拍照并保存在本地
阅读量:7123 次
发布时间:2019-06-28

本文共 2946 字,大约阅读时间需要 9 分钟。

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Calendar;import java.util.Locale;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.text.format.DateFormat;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;@SuppressLint("SdCardPath")public class MainActivity extends Activity {	/** Called when the activity is first created. */	private Button button;	private ImageView view;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		button = (Button) findViewById(R.id.button1);		view= (ImageView)findViewById(R.id.imageView1);		button.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				// TODO Auto-generated method stub				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);				startActivityForResult(intent, 1);			}		});	}	@SuppressLint("SdCardPath")	@Override	protected void onActivityResult(int requestCode, int resultCode, Intent data) {		// TODO Auto-generated method stub		super.onActivityResult(requestCode, resultCode, data);		if (resultCode == Activity.RESULT_OK) {			String sdStatus = Environment.getExternalStorageState();			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检測sd是否可用				Log.i("TestFile",						"SD card is not avaiable/writeable right now.");				return;			}			new DateFormat();			String name = DateFormat.format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";				Toast.makeText(this, name, Toast.LENGTH_LONG).show();			Bundle bundle = data.getExtras();			Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式					FileOutputStream b = null;		   			File file = new File("/sdcard/Image/");			file.mkdirs();// 创建目录			String fileName = "/sdcard/Image/"+name;			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();				}			}			try			{				view.setImageBitmap(bitmap);// 将图片显示在ImageView里			}catch(Exception e)			{				Log.e("error", e.getMessage());			}					}	}}

xml文件

最后要记得加权限

<!-- 在SDCard中创建与删除文件权限 -->

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-feature android:name = "android.hardware.camera" />
<uses-feature android:name = "android.hardware.camera.autofocus" />

转载地址:http://bpael.baihongyu.com/

你可能感兴趣的文章
hdu4570Multi-bit Trie (间隙DP)
查看>>
easyui-datagrid个人实例
查看>>
Java IO4:字符编码
查看>>
dedecms标签的sql语句
查看>>
BZOJ3075 : [Usaco2013]Necklace
查看>>
第七章 过滤器 Filter(二)
查看>>
Hibernate 缓存机制二(转)
查看>>
[chrome插件分享] gitlab-tree 更方便的浏览Gitlab上的代码
查看>>
LintCode: Longest Words
查看>>
Edge Animate初篇教程(二)
查看>>
[转] ServletContext 与application的异同
查看>>
JavaScript动态更改页面元素
查看>>
python --内存管理
查看>>
js操作cookie 使用详解
查看>>
HDU1035深度搜索
查看>>
最全的常用正则表达式大全分享
查看>>
ZOJ 3209 Treasure Map DLX
查看>>
BZOJ3640 : JC的小苹果
查看>>
SessionId
查看>>
自己选的路,跪着也要走完
查看>>