首页 > 代码库 > 调用系统相册或相机工具类
调用系统相册或相机工具类
需求:点击修改头像,弹出对话框提示选择相册还是相机,从而调用系统相册或相机
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; }}
调用系统相册或相机工具类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。