拍照或者从相册选择工具类

package com.iasku.study.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.iasku.iaskuseniormath.R;
import com.iasku.study.activity.personal.PersonalSettingActivity;
import com.iasku.study.common.manage.DataManager;
import com.iasku.study.http.ReturnDataListener;
import com.iasku.study.model.ReturnData;
import com.iasku.study.model.UserDetail;
import com.lidroid.xutils.exception.HttpException;
import com.tools.util.BitmapUtil;
import com.tools.util.LogUtil;
import com.tools.widget.CustomDialog;
import com.umeng.socialize.facebook.controller.utils.ToastUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/7/22 0022.
 * 拍照或者从相册选择工具类
 */
public class PictureUtil {

    public static final int NO_SDCARD = -1; // 没有SD卡
    public static final int PHOTO_REQUEST_TAKEPHOTO = 1; // 拍照
    public static final int PHOTO_REQUEST_GALLERY = 2; // 从相册中选择
    public static final int PHOTO_REQUEST_CUT = 3; // 结果

    public Dialog mDialog;
    private Activity mCtx;
    private Bitmap mBitmap;   // 保存裁剪之后的结果
    private File mSaveFile;     // 临时文件(待上传)
    private String mUploadUrl;       // 上传图片接口地址
    private Map<String, String> params;     // 上传图片其他参数(除了file)
    private Handler mHandler;
    static UserDetail userDetail;

    public PictureUtil(Activity context, File saveFile) {
        mCtx = context;
        mSaveFile = saveFile;
    }

    /**
     * 显示对话框
     */
    public void showPhotoDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(mCtx);
        View view = mCtx.getLayoutInflater().inflate(R.layout.personal_update_avatar_dialog, null);
        TextView captureTv = (TextView) view.findViewById(R.id.dialog_capture);
        TextView galleryTv = (TextView) view.findViewById(R.id.dialog_gallery);
        TextView cancelTv = (TextView) view.findViewById(R.id.dialog_cancel);
        captureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                // 调用系统的拍照功能
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // 指定调用相机拍照后照片的储存路径
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mSaveFile));
                mCtx.startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
            }
        });

        galleryTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                try {
                    mCtx.startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
                } catch (android.content.ActivityNotFoundException e) {
                    ToastUtil.showToast(mCtx, "您的系统没有找到相册软件!");
                }
            }
        });

        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mDialog = builder.setContentView(view).create();
        mDialog.show();
    }

    /**
     * 裁剪图片
     *
     * @param uri  图片文件uri
     * @param size 裁剪后的宽高
     */
    public void cropPic(Uri uri, int size) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // crop为true是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");

        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", size);
        intent.putExtra("outputY", size);
        intent.putExtra("return-data", true);

        mCtx.startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    /**
     * @param picdata 系统对图片裁剪之后的返回结果
     */
    public void savePic(Intent picdata) {
        Bundle bundle = picdata.getExtras();
        if (bundle != null) {
            mBitmap = bundle.getParcelable("data");
            BitmapUtil.saveBitmap(mBitmap, mSaveFile, 60, "jpg");
            if (mBitmap != null && !mBitmap.isRecycled()) {
                mBitmap.recycle();
            }
        }
    }

    public Bitmap savePic2(Intent picdata) {
        Bundle bundle = picdata.getExtras();
        if (bundle != null) {
            mBitmap = bundle.getParcelable("data");
            BitmapUtil.saveBitmap(mBitmap, mSaveFile, 60, "jpg");
            if (mBitmap != null && !mBitmap.isRecycled()) {
                mBitmap.recycle();
            }
        }
        return mBitmap;
    }

    Context context;
    String errorMsg;

    public void uploadPic(final PersonalSettingActivity personalSettingActivity, String uploadUrl, Map<String, String> mapParme, Handler handler) {
        mUploadUrl = uploadUrl;
        params = mapParme;
        mHandler = handler;
        context = personalSettingActivity;
        errorMsg = personalSettingActivity.getString(R.string.personal_avatar_upload_fail);

        Map<String, File> fileMap = new HashMap<String, File>();
        fileMap.put("avatar", mSaveFile);
        DataManager.sendRequest(context, mUploadUrl, new ReturnDataListener<UserDetail>() {
            @Override
            public void onStart(String url) {
                // show loading
            }

            @Override
            public void onErrorResponse(HttpException error) {
                personalSettingActivity.showToast(errorMsg);
            }

            @Override
            public void onResponse(ReturnData<UserDetail> res) {
                if (res.isSuccess()) {
                    userDetail = res.getData();
                    Message msg = mHandler.obtainMessage();
                    msg.obj = userDetail;
                    mHandler.sendMessage(msg);
                }
            }
        }, new TypeReference<UserDetail>() {
        }.getType(), params, fileMap);

        LogUtil.d("resule=" + userDetail);
    }


}


对应的layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@drawable/bg_bombbox"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_title"
        android:gravity="center"
        android:minHeight="45dp"
        android:text="@string/personal_update_avatar"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/dialog_capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_white_to_gray_selector"
        android:gravity="center"
        android:minHeight="50dp"
        android:text="@string/personal_avatar_capture" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#DEDEDE" />

    <TextView
        android:id="@+id/dialog_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_white_to_gray_selector"
        android:gravity="center"
        android:minHeight="50dp"
        android:text="@string/personal_avatar_gallery" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#DEDEDE" />

    <TextView
        android:id="@+id/dialog_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_white_to_gray_selector"
        android:gravity="center"
        android:minHeight="50dp"
        android:text="@string/common_no" />

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值