Android调用系统相机和图库

程序截图

只实现了调用系统相机和本地图库,上传图片将在下一篇博客实现,这里推荐一个获取Android按钮图片的插件Android Drawable Importer

主要过程

(一)添加读写权限

这里是调用系统相机,不需要获取相机权限。

name="android.permission.WRITE_EXTERNAL_STORAGE"/>name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

(二)调用系统相机和图库

1.调用系统相机
这里是使用Intent的参数MediaStore.ACTION_IMAGE_CAPTURE 来实现的,然后调用startActivityForResult 方法实现对返回的数据进行处理。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//存放相机返回的图片File file = new File(filePath);if(file.exists()){file.delete()}Uri uri = Uri.fromFile(file);intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);startActivityForResult(intent1,TAKE_PHOTO);

2.调用图库
这里是使用Intent的Action参数Intent.ACTION_GET_CONTENT 来实现的

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);//选择图片格式intent.setType("image/*");intent.putExtra("return-data",true);startActivityForResult(intent,CHOOSE_PHOTO);

(三)处理返回数据

调用onActivityResult 方法分别对于图库和相机,以及对于下一项介绍的图片剪裁后返回的图片进行处理

protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode){            //处理图库返回            case CHOOSE_PHOTO:                if(resultCode == RESULT_OK){                    photoZoom(data.getData());                }                break;            //处理相机返回            case TAKE_PHOTO:                if(resultCode == RESULT_OK){                    File file = new File(filePath);                    photoZoom(Uri.fromFile(file));                }            //处理裁剪返回            case PHOTO_RESULT:                Bundle bundle = new Bundle();                try {                    bundle = data.getExtras();                    if (resultCode == RESULT_OK) {                        Bitmap bitmap = bundle.getParcelable("data");                        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, new ByteArrayOutputStream());                        //修改ImageView的图片                        photoImage.setImageBitmap(bitmap);                    }                } catch (java.lang.NullPointerException e) {                    e.printStackTrace();                }                break;        }

(四)剪裁图片

通过Intent 调用com.android.camera.action.CROP 系统的剪裁功能。

    public void photoZoom(Uri uri) {        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, "image/*");        intent.putExtra("crop", "true");        // aspectX aspectY 是宽高的比例        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        // outputX outputY 是裁剪图片宽高        intent.putExtra("outputX", 300);        intent.putExtra("outputY", 130);        intent.putExtra("return-data", true);        startActivityForResult(intent, PHOTO_RESULT);    }

更多相关文章

  1. android mediaplayer
  2. android 4.04的应用程序启动过程及与Zygote的交互(基于静态源码分
  3. Android(安卓)照相机
  4. Android(安卓)四大组件 简介
  5. Android中native进程内存泄露的调试技巧
  6. Android(安卓)JNI
  7. Android(安卓)Power Management
  8. Android(安卓)AsyncTask实现异步任务的执行
  9. Android(安卓)实现QQ第三方登录

随机推荐

  1. rockchip rk3368(px5)车载开发之路-bug解
  2. Android Bluetooth How To--Based on And
  3. Android和。net加密。
  4. Android中高效的显示图片之三——缓存图
  5. Android Studio(四):Android Studio集成Ge
  6. 安卓如何限制横屏和竖屏
  7. Android 学习网站汇总
  8. Android去掉头部标题
  9. android大屏设备中的经典应用--MIQI卡片
  10. Android:configChanges 属性