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基础学习--->Android SharedPreferences存储对象和图片(An
  2. Android根据Button状态(normal,focused,pressed)显示不同背景图
  3. Android 高清加载巨图方案 拒绝压缩图片
  4. Android 照相机
  5. Android图片加载神器之Fresco,基于各种使用场景的讲解
  6. Android(1.5及以上版本) 开机图片/文字/动画分析
  7. Android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片
  8. Android沉浸式状态栏+图片背景+标题栏渐变+背景伸缩
  9. Android--高效地加载大图片

随机推荐

  1. Android作为HTTP服务器--NanoHTTPD源码分
  2. Android 播放提示音
  3. android中调用相册里面的图片并返回
  4. 录音及播放音频文件
  5. jamendo_android 一个开源的Android在线
  6. android中的一个属性动画,可以显示更多的
  7. Android利用Looper在子线程中改变UI
  8. 编程回忆之Android回忆(Android应用参数的
  9. android 判断 网络 类型
  10. 如何把android设备中的固件dump出来