Android 调用系统相机、相册

需求:

一、点击弹出popwindow

二、选择打开相机 或 打开相册

三、拍照进行展示 或 选择照片进行展示

四、展示完后保存当前相册,存储到SharedPreferences

五、重新打开项目,直接进行展示SharedPreferences中的图片

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
        

        //展示sp中的相册
        bitmap = null;
        SharedPreferences sharedPre=getSharedPreferences("Image", MODE_PRIVATE);
        String icon = sharedPre.getString("icon", "");
        if(icon != "") {
            byte[] decode = Base64.decode(icon.getBytes(), 1);
            bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
            imageView.setImageBitmap(bitmap);
        }

        //点击弹出popopwindow
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View contentView=View.inflate(MainActivity.this, R.layout.popupwindow, null);
                PopupWindow pop=new PopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
                pop.setOutsideTouchable(true);
                pop.setFocusable(true);
                pop.setBackgroundDrawable(new ColorDrawable(0x00000000));
                pop.showAsDropDown(v);
                //相机
                contentView.findViewById(R.id.camera).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent, 22);
                    }
                });
                //相册裁剪
                contentView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent=new Intent(Intent.ACTION_PICK);
                        intent.setType("image/*");
                        startActivityForResult(intent, 23);
                    }
                });
            }

        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 22:
                //相机拍完照片返回展示
                Bitmap bitmap = data.getParcelableExtra("data");
                imageView.setImageBitmap(bitmap);

                //SharedPreferences存储Bitmap图片 首先需要将图片写成字节流,转换为String字符串,将字符串存入SharedPreferences中。
                SharedPreferences sharedPre=getSharedPreferences("Image", MODE_PRIVATE);

                //获取Editor对象
                SharedPreferences.Editor editor=sharedPre.edit();


                Bitmap bit = data.getParcelableExtra("data");
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bit.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream);
                String headimg = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT));

                editor.putString("icon",headimg);
                editor.commit();
                break;
            case 23:
                //进入裁剪页面,裁剪完毕后返回22进行展示
                Uri uri = data.getData();
                Intent crop = crop(uri);
                startActivityForResult(crop, 22);

                //SharedPreferences存储Bitmap图片 首先需要将图片写成字节流,转换为String字符串,将字符串存入SharedPreferences中。
                SharedPreferences sharedPre2=getSharedPreferences("Image", MODE_PRIVATE);

                //获取Editor对象
                SharedPreferences.Editor editor2=sharedPre2.edit();

                Bitmap bit2 = data.getParcelableExtra("data");
                ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
                bit2.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream2);
                String headimg2 = new String(Base64.encodeToString(byteArrayOutputStream2.toByteArray(),Base64.DEFAULT));

                editor2.putString("icon",headimg2);
                editor2.commit();
                break;
        }
    }
    public Intent crop(Uri uri){
        Intent intent=new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");
        intent.putExtra("crop", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        intent.putExtra("outputFromet", "JPEG");
        intent.putExtra("return-data", true);
        return intent;
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值