android获取拍照图片、本地图片简单实现!
2016-05-25 13:45
585 查看
在安卓应用开发中经常会用到调用系统相机拍照跟获取本地图片功能,下面就是对这一常用功能的简单实现Demo!
在调用拍照功能跟自定义图片路径时中要加上这两权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
布局文件
代码
上面代码只获取了图片,如果要获取图片其它信息,比如拍摄时间,图片名等就办不到了,因为android4.4版本获取到的路径变了所以就需要考虑其4.4以上版本的兼容问题了,其实也挺简单,代码如下。
附Demo下载链接
http://download.csdn.net/detail/u013740033/9530537
在调用拍照功能跟自定义图片路径时中要加上这两权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
布局文件
<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="com.hhkb.obtainmobilephoto.MainActivity" > <Button android:id="@+id/btn_album" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/album" /> <Button android:id="@+id/btn_camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/btn_album" android:text="@string/camera" /> <ImageView android:id="@+id/iv_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_camera" android:src="@drawable/pretty" /> </RelativeLayout>
代码
public class MainActivity extends Activity implements OnClickListener { private static final int CAMERA = 1025; private static final int ALBUM = 1026; TextView mBtnAlbum, mBtnCamera; ImageView mIvPhoto; private File mFolder; private String mImgName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); initData(); } private void initView() { setContentView(R.layout.activity_main); mBtnAlbum = (TextView) findViewById(R.id.btn_album); mBtnCamera = (TextView) findViewById(R.id.btn_camera); mIvPhoto = (ImageView) findViewById(R.id.iv_photo); } private void initData() { mBtnAlbum.setOnClickListener(this); mBtnCamera.setOnClickListener(this); } /* * 设置从相机获取图片 */ private void getImgFromCamra() { String state = Environment.getExternalStorageState(); // 先检测是不是有内存卡。 if (state.equals(Environment.MEDIA_MOUNTED)) { mFolder = new File(Environment.getExternalStorageDirectory(), "bCache"); // 判断手机中有没有这个文件夹,没有就新建。 if (!mFolder.exists()) { mFolder.mkdirs(); } // 自定义图片名字,这里是以毫秒数作为图片名。 mImgName = System.currentTimeMillis() + ".jpg"; Uri uri = Uri.fromFile(new File(mFolder, mImgName)); // 调用系统拍照功能。 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, CAMERA); } else { Toast.makeText(this, "未检测到SD卡", Toast.LENGTH_SHORT).show(); } } /* * 设置从本地相册获取图片 */ private void getImgFromAlbum() { // 调用本地图库。 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, ALBUM); } public void onActivityResult(int requestCode, int resultCode, Intent data) { Bitmap bm; if (resultCode == RESULT_OK && requestCode == CAMERA) { // 调用系统方法获取到的是被压缩过的图片,通过自定义路径轻松获取原始图片。 bm = BitmapFactory.decodeFile(mFolder.getAbsolutePath() + File.separator + mImgName); mIvPhoto.setImageBitmap(bm); } if (resultCode == RESULT_OK && requestCode == ALBUM) { try { if (data != null) { // 获取本地相册图片。 Uri uri = data.getData(); ContentResolver cr = getContentResolver(); bm = BitmapFactory.decodeStream(cr.openInputStream(uri)); mIvPhoto.setImageBitmap(bm); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_album: getImgFromAlbum(); break; case R.id.btn_camera: getImgFromCamra(); break; default: break; } }; }
上面代码只获取了图片,如果要获取图片其它信息,比如拍摄时间,图片名等就办不到了,因为android4.4版本获取到的路径变了所以就需要考虑其4.4以上版本的兼容问题了,其实也挺简单,代码如下。
@SuppressLint("NewApi") public void onActivityResult(int requestCode, int resultCode, Intent data) { Bitmap bm; if (resultCode == RESULT_OK && requestCode == CAMERA) { // 调用系统方法获取到的是被压缩过的图片,通过自定义路径轻松获取原始图片。 bm = BitmapFactory.decodeFile(mFolder.getAbsolutePath() + File.separator + mImgName); mIvPhoto.setImageBitmap(bm); } if (resultCode == RESULT_OK && requestCode == ALBUM) { try { if (data != null) { ContentResolver cr = getContentResolver(); Cursor cursor; String[] column = { MediaStore.Images.Media.DATA };//图片路径列名 Uri imgURI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; String imgPath = "";//定义图片路径 final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; Uri uri = data.getData(); // 获取本地相册图片。 if (isKitKat && DocumentsContract.isDocumentUri(this, uri)) {// 判断版本号 String id = DocumentsContract.getDocumentId(uri).split( ":")[1];// 获取图片id String sel = MediaStore.Images.Media._ID + "=?"; cursor = cr.query(imgURI, column, sel, new String[] { id }, null); } else { cursor = cr.query(uri, column, null, null, null); } cursor.moveToFirst(); imgPath = cursor.getString(0); cursor.close(); bm = BitmapFactory.decodeFile(imgPath); mIvPhoto.setImageBitmap(bm); } } catch (Exception e) { e.printStackTrace(); } } }
附Demo下载链接
http://download.csdn.net/detail/u013740033/9530537
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories