自定义dialog,点击用隐式Intent跳转调用系统相机相册。

这篇博客介绍了如何在点击头像时通过自定义Dialog和隐式Intent实现调用系统相机和相册的功能。用户交互过程中,Dialog弹出并提供选择,回调机制确保了用户操作的顺利进行。

点击头像弹出
自定义dialog

private void alertDialog() {
        final String[] items = { "相册","相机" };
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(getContext());
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 0:
                        Intent intent = new Intent(Intent.ACTION_PICK);
                        intent.setType("image/*");
                        startActivityForResult(intent,200);
                        break;
                        case 1:
                            Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            intent1.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path)));
                            startActivityForResult(intent1,300);
                            break;
    
                    }
                }
            }).show();
 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==300&&resultCode==RESULT_OK){
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(Uri.fromFile(new File(path)),"image/*");
            intent.putExtra("CROP",true);
            intent.putExtra("aspectX",1);
            intent.putExtra("aspectY",1);
            intent.putExtra("outputX",100);
            intent.putExtra("outputX",100);
            intent.putExtra("return-data",true);
            startActivityForResult(intent,400);
        }

        if(requestCode==200&&resultCode==RESULT_OK){
            Intent intent = new Intent("com.android.camera.action.CROP");
            Uri uri = data.getData();
            intent.setDataAndType(uri,"image/*");
            intent.putExtra("CROP",true);
            intent.putExtra("aspectX",1);
            intent.putExtra("aspectY",1);
            intent.putExtra("outputX",100);
            intent.putExtra("outputX",100);
            intent.putExtra("return-data",true);
            startActivityForResult(intent,400);
        }

        if(requestCode==400&&resultCode==RESULT_OK){

            Bitmap bitmap = data.getParcelableExtra("data");
            File file=new File(path);//将要保存图片的路径
            try {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String,String> params=new HashMap<>();
        params.put("file",path);
        iPersenter.postFiles(dataUrl,params,FliesBean.class);
    }

点击头像弹出popwindow

 private void popWindow() {
        View view=View.inflate(getContext(),R.layout.image,null);
        TextView text_camcme = view.findViewById(R.id.cancme);
        TextView text_pick = view.findViewById(R.id.pick);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.showAtLocation(view,
                Gravity.BOTTOM, 0, 0);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);

        text_camcme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
                startActivityForResult(intent, 400);
                popupWindow.dismiss();
            }
        });
        text_pick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 300);
                popupWindow.dismiss();
            }
        });

    }

回调

//相机
     @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode==400&&resultCode==RESULT_OK) {
                //打开裁剪
                Intent intent = new Intent("com.android.camera.action.CROP");
                //将图片设置给裁剪
                intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
                //设置是否支持裁剪
                intent.putExtra("CROP", true);
                //设置宽高比
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                //设置输出后图片大小
                intent.putExtra("outputX", 100);
                intent.putExtra("outputY", 100);
                //返回到data
                intent.putExtra("return-data", true);
                //启动
                startActivityForResult(intent, 200);
        }
        
    //相册
    if (requestCode==300&&resultCode==RESULT_OK) {
        //打开裁剪
        Intent intent = new Intent("com.android.camera.action.CROP");
        Uri uri=data.getData();
        //将图片设置给裁剪
        intent.setDataAndType(uri, "image/*");
        //设置是否可裁剪
        intent.putExtra("CROP", true);
        //设置宽高比
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        //设置输出
        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        //返回data
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 200);
    }

    if (requestCode==200&&resultCode==RESULT_OK) {

        Bitmap bitmap = data.getParcelableExtra("data");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 500) {  //循环判断如果压缩后图片是否大于500kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            options -= 10;//每次都减少10
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
        }
        File file = new File(files);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            try {
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        recycleBitmap(bitmap);
    }
       Map<String,String> params = new HashMap<>();
        //TODO  头像路径
        params.put("image",files);

        iPersenter.postFiles(Apis.TYPR_INMAGE_XINXI,params,HeadPicBean.class);

}

public static void recycleBitmap(Bitmap... bitmaps) {
    if (bitmaps==null) {
        return;
    }
    for (Bitmap bm : bitmaps) {
        if (null != bm && !bm.isRecycled()) {
            bm.recycle();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值