拍照,系统选择照片

//选择照片
Intent iphoto = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
iphoto.setType(“image/*”);
startActivityForResult(iphoto, Constant.REQUEST_SELECTPHOTO);
//拍照
Intent iPNow = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(iPNow, Constant.REQUEST_TAKEPHOTO);

//onActivityResult
//diaryPhoto  照片路径
// 照片
        case Constant.REQUEST_SELECTPHOTO:
            diaryPhoto= getSelectMediaPath(data);
            setMediaPhoto();
            break;
        case Constant.REQUEST_TAKEPHOTO:
            Bitmap bm = (Bitmap) data.getExtras().get("data");
            if (bm != null) {
                diaryPhoto = ImageUtils.savePicture(bm, TABKEPHOTO_PATH);
            }
            setMediaPhoto();
            break;




//获取选择图片路径
    private String getSelectMediaPath(Intent data){
    String path ="";
    Uri uri = data.getData(); // 获取别选中图片的uri
    if (uri.toString().contains("file:///")) {
        path = uri.toString().replace("file:///", "");
    } else {
        String[] filePathColumn = { MediaStore.Images.Media.DATA }; // 获取图库图片路径
        Cursor cursor = getContentResolver().query(uri,
                filePathColumn, null, null, null); // 查询选中图片路径
        cursor.moveToFirst();
        path = cursor.getString(cursor
                .getColumnIndex(filePathColumn[0]));
        cursor.close();
    }
    return path;
}



 /**
 * 保存图片
 * */
   public static  String savePicture(Bitmap bm,String path){
    File file2 = null;
    if(SDCardUtil.isExsit()){
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        file2 = new File(file,System.currentTimeMillis()+".jpg");
        FileOutputStream fos;
        try { 
            fos = new FileOutputStream(file2);
            if(bm != null){
                bm.compress(CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    return file2.getAbsolutePath();
   }



   //获取清晰图片防止oom
   String path= getSelectMediaPath(data);
            if (!TextUtils.isEmpty(path)) {
                try{
                    WindowManager wm = this.getWindowManager();
                    Display display = wm.getDefaultDisplay();
                    int wWidth = display.getWidth();
                    int wHeight = display.getHeight();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(path, options);
                    options.inSampleSize = 1;
                    if(options.outWidth > wWidth || options.outHeight > wHeight){
                        options.inSampleSize = options.outWidth / wWidth;
                        if(options.inSampleSize < options.outHeight / wHeight)
                            options.inSampleSize = options.outHeight / wHeight;
                    }
                    options.inJustDecodeBounds = false;
                    photoBmp = BitmapFactory.decodeFile(path, options);

                }catch(OutOfMemoryError oom){
                    return;
                }
                setDefinedSkin(photoBmp);
            }`这里写代码片`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值