相机相册

MainActivity

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("提示");
                builder.setPositiveButton("选择系统相册", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_PICK);
                        intent.setType("image/*");
                        startActivityForResult(intent, 20);
                    }
                });

                builder.setNeutralButton("选择系统相机", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent1.addCategory("android.intent.category.DEFAULT");
                        startActivityForResult(intent1, 10);
                    }
                });
                builder.show();

onActivityResult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 10 && resultCode == getActivity().RESULT_OK) {
            Bitmap bitmap = data.getParcelableExtra("data");
            mImageVIew.setImageBitmap(bitmap);
            //bitmap 转换 Uri
            Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContext().getContentResolver(), bitmap, null,null));
        }
        if (requestCode == 20 && resultCode == getActivity().RESULT_OK) {
            Uri uri = data.getData();
//            mImageVIew.setImageURI(uri);
        }
    }

权限

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
发布了37 篇原创文章 · 获赞 15 · 访问量 5610
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览