码迷,mamicode.com
首页 > 其他好文 > 详细

调用系统相册或相机工具类

时间:2016-10-16 01:31:50      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

需求:点击修改头像,弹出对话框提示选择相册还是相机,从而调用系统相册或相机

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Toast;

import com.xuehu365.xuehu.model.Constant;
import com.xuehu365.xuehu.ui.widget.AlertDialog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by Administrator on 2016/10/15.
 */
public class GetPhotoUtils {

    //相片来源类型
    public static String touxiangType;

    //弹出选择相片来源对话框
    public static void showDialog(final Context context) {
        AlertDialog alertDialog = new AlertDialog(context).builder();
        alertDialog.setTitle("选择相片来源").setPositiveButton("相册", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用相册
                touxiangType = Constant.picture;
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                ((Activity) context).startActivityForResult(intent, Constant.IMAGE);
            }
        }).setNegativeButton("去照相", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用照相机
                touxiangType = Constant.photo;
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                ((Activity) context).startActivityForResult(intent, Constant.IMAGE);
            }
        }).show();
    }

    //回调加载图片
    public static Bitmap setBitmap(int resultCode, Intent data, Context context) {
        //判断是来自相册还是相机
        if (touxiangType.equals(Constant.photo)) {
            if (resultCode == Activity.RESULT_OK) {
                String sdStatus = Environment.getExternalStorageState();
                if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
                    return null;
                }
                new DateFormat();
                String name = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
                Toast.makeText(context, name, Toast.LENGTH_LONG).show();
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式

                FileOutputStream b = null;
                File file = new File("/sdcard/Image/");
                file.mkdirs();// 创建文件夹
                String fileName = "/sdcard/Image/" + name;

                try {
                    b = new FileOutputStream(fileName);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        b.flush();
                        b.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return bitmap;
            }
        } else if (touxiangType.equals(Constant.picture)) {
            //获取图片路径
            if (/*requestCode == Constant.IMAGE && */resultCode == Activity.RESULT_OK && data != null) {
                Uri selectedImage = data.getData();
                String[] filePathColumns = {MediaStore.Images.Media.DATA};
                Cursor c = context.getContentResolver().query(selectedImage, filePathColumns, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePathColumns[0]);
                String imagePath = c.getString(columnIndex);
                Bitmap bitmap = BitmapUtils.getBitmap(imagePath, 240, 240);
                c.close();
                return bitmap;
            }
        }
        return null;
    }
}

 

调用系统相册或相机工具类

标签:

原文地址:http://www.cnblogs.com/anni-qianqian/p/5965664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!