在网上整理的关于打开相机和相册并设置成头像的问题

来源:互联网 发布:ubuntu查找已安装软件 编辑:程序博客网 时间:2024/05/22 11:33

在网上找了好几个demo 可是运行起来都是有问题 很多都是相册的图片无法设置成头像 在此整理一下

首先 加权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />    <!-- 在SDCard中创建与删除文件权限 -->    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <!-- 往SDCard写入数据权限 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <!-- 访问internet权限 -->    <uses-permission android:name="android.permission.INTERNET" />    <!-- 照相机权限权限 -->    <uses-permission android:name="android.permission.CAMERA" />
 private static final int CAMERA_WITH_DATA = 1;//相机 private static final int PHOTO_REQUEST = 2;//相册 private static final int PHOTO_PICKED_WITH_DATA = 3;//裁剪 private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");//图片的存储目录 //用当前时间给取得的图片命名    private String getPhotoFileName() {        Date date = new Date(System.currentTimeMillis());        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");        return dateFormat.format(date) + ".jpg";    }//打开相机并拍照 然后裁剪图片PHOTO_DIR.mkdir();mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName()); //用当前时间给取得的图片命名//拍照if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {                            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile));                            startActivityForResult(intent, CAMERA_WITH_DATA);     } else {                            Toast.makeText(MyProfileActivity.this, "没有sd卡", Toast.LENGTH_LONG);                    } //调用系统相册                        Intent intent = new Intent(Intent.ACTION_PICK, null);                        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,                                "image/*");                        startActivityForResult(intent, PHOTO_REQUEST);    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case CAMERA_WITH_DATA://相机返回来的数据                try {                    // 启动gallery去剪辑这个照片                    getCropImageIntent(Uri.fromFile(mCurrentPhotoFile));                } catch (Exception e) {                    Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();                }                break;            case PHOTO_REQUEST://相册返回来的数据                if (data.getData() != null) {                    try {                        // 启动gallery去剪辑这个照片                        getCropImageIntent(data.getData());                    } catch (Exception e) {                        Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();                    }                }else {                    Toast.makeText(this, "取消修改头像", Toast.LENGTH_LONG).show();                }                break;            case PHOTO_PICKED_WITH_DATA://裁剪返回来的数据                Bundle extras = data.getExtras();                if (extras != null) {                    Bitmap bitmap = (Bitmap) extras.get("data");                    headerImageView.setImageBitmap(bitmap);                    SavePicInLocal(bitmap);                } else {                    Toast.makeText(this, "取消上传头像", Toast.LENGTH_SHORT).show();                }                break;        }    }    /**     * Constructs an intent for image cropping. 调用图片剪辑程序     * 剪裁后的图片跳转到新的界面     */    public void getCropImageIntent(Uri photoUri) {        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(photoUri, "image/*");        intent.putExtra("crop", "true");        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        intent.putExtra("outputX", 64);        intent.putExtra("outputY", 64);        intent.putExtra("return-data", true);        startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);    }
0 0