解决7.0拍照问题、图片选择

package com.up72.library.picture;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.IntDef;
import android.support.v7.app.AlertDialog;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 图片选择
 * Created by hrd on 2016/6/17.
 */
public class Picture {
    /**
     * requestCode
     */
    public static final int REQUEST_CODE = 16154;
    /**
     * 文件的绝对路径
     */
    public static final String IMAGE_PATH = "IMAGE_PATH";
    /**
     * 文件的绝对路径
     */
    public static final String VIDEO_PATH = "VIDEO_PATH";
    private Context mContext;
    private Intent mIntent;

    interface Extra {
        String DEFAULT_WIDTH = "DEFAULT_WIDTH";
        String DEFAULT_HEIGHT = "DEFAULT_HEIGHT";
        String MAX_WIDTH = "MAX_WIDTH";
        String MAX_HEIGHT = "MAX_HEIGHT";
        String IS_CROP = "IS_CROP";
        String MAX_KB = "MAX_KB";
        String OPEN_TYPE = "OPEN_TYPE";
    }

    public static Picture of(Context context) {
        return new Picture(context);
    }

    private Picture() {
    }

    private Picture(Context context) {
        this.mContext = context;
        this.mIntent = new Intent();
    }

    /**
     * 裁剪
     */
    public Picture crop() {
        this.mIntent.putExtra(Extra.IS_CROP, true);
        return this;
    }

    /**
     * 设置裁剪比例,例:1:2 —— withAspect(1,2)
     * </br>
     * 需先调用crop才有效
     *
     * @param width       * @param height      */
    public Picture withAspect(int width, int height) {
        this.mIntent.putExtra(Extra.DEFAULT_WIDTH, width);
        this.mIntent.putExtra(Extra.DEFAULT_HEIGHT, height);
        return this;
    }

    /**
     * 设置正方形裁剪
     * </br>
     * 需先调用crop才有效
     */
    public Picture asSquare() {
        this.mIntent.putExtra(Extra.DEFAULT_WIDTH, 1);
        this.mIntent.putExtra(Extra.DEFAULT_HEIGHT, 1);
        return this;
    }

    /**
     * 设置裁剪图片后,图片的最大宽高
     *
     * @param width  最大宽
     * @param height 最大高
     */
    public Picture withMaxSize(int width, int height) {
        this.mIntent.putExtra(Extra.MAX_WIDTH, width);
        this.mIntent.putExtra(Extra.MAX_HEIGHT, height);
        return this;
    }

    /**
     * 设置图片最大大小
     *
     * @param maxKB 最大大小,单位KB
     */
    public Picture withMaxKB(long maxKB) {
        this.mIntent.putExtra(Extra.MAX_KB, maxKB);
        return this;
    }

    public void start() {
        start(REQUEST_CODE, Type.DEFAULT);
    }

    public void start(@Type int type) {
        start(REQUEST_CODE, type);
    }

    public void start(int requestCode, @Type int type) {
        if (mContext instanceof Activity) {
            mIntent.putExtra(Extra.OPEN_TYPE, type);
            Activity activity = (Activity) mContext;
            mIntent.setClass(activity, PictureActivity.class);
            activity.startActivityForResult(mIntent, requestCode);
            activity.overridePendingTransition(0, 0);
        }
    }

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({Type.DEFAULT, Type.CAMERA, Type.PHOTO,Type.VIDEO})
    public @interface Type {
        /**
         * 默认,手动选择
         */
        int DEFAULT = 0;
        /**
         * 相机
         */
        int CAMERA = 1;
        /**
         * 相册
         */
        int PHOTO = 2;
        /**
         * 录制
         */
        int VIDEO = 3;
    }
    /**
     * 检查是否有拍照权限
     *
     * @param activity
     * @return
     */
    public static boolean isCameraCanUse(final Activity activity) {
        boolean canUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
            // setParameters 是针对魅族MX5 做的。MX5 通过Camera.open() 拿到的Camera
            // 对象不为null
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            canUse = false;
        }
        if (mCamera != null) {
            mCamera.release();
        }
        if (!canUse) {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("提示");
            builder.setMessage("您没有开启相机权限,请设置开启");
            builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent localIntent = new Intent();
                    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    if (Build.VERSION.SDK_INT >= 9) {
                        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                        localIntent.setData(Uri.fromParts("package", activity.getPackageName(), null));
                    } else if (Build.VERSION.SDK_INT <= 8) {
                        localIntent.setAction(Intent.ACTION_VIEW);
                        localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                        localIntent.putExtra("com.android.settings.ApplicationPkgName", activity.getPackageName());
                    }
                    activity.startActivity(localIntent);
                }
            });
            builder.setNegativeButton("取消", null);
            builder.show();
        }
        return canUse;
    }
}
 
// TODO: 2016/7/28 设置头像  使用

if(Picture.isCameraCanUse(MyInfoActivity.this)){

    Picture.of(this).crop().asSquare().start(Picture.Type.DEFAULT);
}

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值