博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android拍照相册工具类
阅读量:4282 次
发布时间:2019-05-27

本文共 5702 字,大约阅读时间需要 19 分钟。

package com.zwk.ezandroid.util;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import android.net.Uri;import android.os.Environment;import android.provider.MediaStore;import android.widget.Toast;import java.io.File;import static android.app.Activity.RESULT_CANCELED;/** *  拍照,照片选取,裁剪,统一封装 */public class WKCameraUtil {    private static final int CAMERA_CODE = 1;    private static final int GALLERY_CODE = 2;    private static final int CROP_CODE = 3;    public Activity act;    private File photoFile;    private Uri photoUri;    public WKCameraUtil(Activity act){//        this(act,new File(App.getInstance().getExternalCacheDir(), "ezandroid\tempPhoto.jpg"));        this(act,new File(App.DirectoryImagePersonPhoto, "tempPhoto.jpg"));    }    public WKCameraUtil(Activity act, File file){        this.act=act;        photoFile = file;    }    public void start(){        String[] type = { act.getResources().getString(R.string.GENERAL_CAMERA),                act.getResources().getString(R.string.GENERAL_SELECT_PICTURE) };        new AlertDialog.Builder(act).setItems(type,                new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        String state = Environment.getExternalStorageState();                        if (!state.equals(Environment.MEDIA_MOUNTED)) {                            Toast.makeText(act, "SDCard不可用,请打开文件读取权限", Toast.LENGTH_SHORT).show();                            dialog.dismiss();                            return;                        }                        switch (which) {                            case 0:                                chooseFromCamera();                                break;                            case 1:                                chooseFromGallery();                                break;                        }                    }                }).show();    }    /**     * 从相册选择图片     */    private void chooseFromGallery() {        photoUri = Uri.fromFile(photoFile);        Intent intent = new Intent(Intent.ACTION_PICK);        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);        intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);        act.startActivityForResult(intent, GALLERY_CODE);    }    // 拍照选择图片    private void chooseFromCamera() {        // 调用系统相机        photoUri = FileUtil.getUriByFile(photoFile);        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);        intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);        act.startActivityForResult(intent, CAMERA_CODE);    }    public void onActivityResult(int requestCode, int resultCode, Intent data, WKCallback call) {        if (resultCode == RESULT_CANCELED) {            T.toast("取消");            return;        }        switch (requestCode) {            case CAMERA_CODE:                startImageZoom(photoUri);                break;            case GALLERY_CODE:                startImageZoom(data.getData());                break;            case CROP_CODE:                call.onCall(Uri.fromFile(photoFile));                break;            default:                break;        }    }    /**     * 通过Uri传递图像信息以供裁剪     * @param uri     */    private void startImageZoom(Uri uri) {        // 构建隐式Intent来启动裁剪程序        Intent intent = new Intent("com.android.camera.action.CROP");        // 设置数据uri和类型为图片类型        intent.setDataAndType(uri, "image/*");        intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);        // 显示View为可裁剪的        intent.putExtra("crop", true);        // 裁剪的宽高的比例为1:1        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        // 输出图片的宽高均为150        intent.putExtra("outputX", 150);        intent.putExtra("outputY", 150);        intent.putExtra("output", Uri.fromFile(photoFile));        // 裁剪之后的数据是通过Intent返回        intent.putExtra("return-data", false);        act.startActivityForResult(intent, CROP_CODE);    }        public interface WKCallback
{ void onCall(T t); }}

使用方式:

WKCameraUtil wkCamera;private OnClickListener linstener = new OnClickListener() {	@Override	public void onClick(View v) {		switch (v.getId()) {		case R.id.iv_person_photo:			wkCamera = new WKCameraUtil(this);			//wkCamera = new WKCameraUtil(this,new File("文件保存路径"));			wkCamera.start();		break;		}	}}; protected void onActivityResult(int requestCode, int resultCode, Intent data) {	wkCamera.onActivityResult(requestCode,resultCode,data,new WKCameraUtil.WKCallback() {		@Override		public void onCall(Uri photoUri) {			Bitmap resultBitmap = BitmapUtil.getBitmap(photoUri, PersonCenterActivity.this);		}	});}public static Bitmap getBitmap(Uri photoUri, Context context) {	BitmapFactory.Options options = new BitmapFactory.Options();	options.inPreferredConfig = Bitmap.Config.RGB_565;	options.inPurgeable = true;	options.inInputShareable = true;	options.inPreferredConfig = Config.RGB_565;	InputStream inputStream = null;	Bitmap resultBitmap = null;	try {		inputStream = context.getContentResolver()				.openInputStream(photoUri);		resultBitmap = BitmapFactory.decodeStream(inputStream, null, options);	} catch (FileNotFoundException e) {		e.printStackTrace();	} finally {		if (null!=inputStream) {			try {				inputStream.close();			} catch (IOException e) {				e.printStackTrace();			}		}	}	return srcBitmap;}

转载地址:http://rfcgi.baihongyu.com/

你可能感兴趣的文章
Data loss in embedded file system UBIFS after shutdown of system
查看>>
watchdog
查看>>
Linux: Find Out How Many File Descriptors Are Being Used
查看>>
kernel panic handle
查看>>
sysrq-trigger
查看>>
ubiformat partition(The partition has data in it)
查看>>
在Linux下使用兩張網卡的設定(gateway)
查看>>
How to list the open file descriptors (and the files they refer to) in my current bash session
查看>>
git alias name
查看>>
在vim中判斷檔案格式,是dos或者unix
查看>>
nmap
查看>>
nmap 與 brutespray合作破解
查看>>
如何設定static route
查看>>
gethostbyname与DNS
查看>>
strace
查看>>
debug tools
查看>>
killall doesn't kill all and rarely kills, what is the command for then?
查看>>
ip数据包经由路由转发的时候源ip MAC,目的ip 目的MAC是否改变
查看>>
How to determine the filesystem of an unmounted device?
查看>>
NAT 相關
查看>>