Android 头像修改

public class AvatarMananger {
    private String AVATAR_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "xd";
    public static final int AVATER_PIC = 1001;
    public static final int AVATER_ALBUM = 1002;
    public static final int AVATER_CROPE = 1003;
    private static AvatarMananger instance;
    private static Activity act;
    public static Uri lastUri;

    private AvatarMananger() {
        deleterFile();
        newDirectory();
    }


    private void newDirectory() {
        File file = new File(AVATAR_PATH);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public static AvatarMananger newInstance(Activity activity) {
        act = activity;
        if (null == instance)
            instance = new AvatarMananger();
        return instance;
    }

    /**
     * 打开图库
     */
    public void openAlbum() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        act.startActivityForResult(intent, AVATER_ALBUM);
    }

    /**
     * 拍照
     */
    public void takePic() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        lastUri = Uri.fromFile(new File(AVATAR_PATH + "/" + System.currentTimeMillis() + ".jpg"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, lastUri);
        act.startActivityForResult(intent, AVATER_PIC);
    }

    /**
     * 裁剪
     */
    public void cropePic(Uri uri, int outputX, int outputY) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);//黑边
        intent.putExtra("scaleUpIfNeeded", true);//黑边
        intent.putExtra("return-data", false);
        lastUri = Uri.fromFile(new File(AVATAR_PATH + "/" + System.currentTimeMillis() + ".jpg"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, lastUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        act.startActivityForResult(intent, AVATER_CROPE);
    }


    private void deleterFile() {
        FileUtils.deleteDir(AVATAR_PATH);
    }


    public static Uri getLastUri() {
        return lastUri;
    }

包含相册,拍照,裁剪等功能,回调在使用Activity的onActivityResult中。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK)
            switch (requestCode) {
                case AvatarMananger.AVATER_ALBUM:
//                    ivShow.setImageURI(data.getData());
                    AvatarMananger.newInstance(TestActivity.this).cropePic(data.getData(), 200, 200);
                    break;
                case AvatarMananger.AVATER_PIC:
//                    ivShow.setImageURI(AvatarMananger.newInstance(TestActivity.this).getLastUri());
                    AvatarMananger.newInstance(TestActivity.this).cropePic(AvatarMananger.newInstance(TestActivity.this).getLastUri(), 200, 200);
                    break;
                case AvatarMananger.AVATER_CROPE:
                    ivShow.setImageURI(AvatarMananger.newInstance(TestActivity.this).getLastUri());
                    break;
            }
    }

你可能感兴趣的:(android)