欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

兼容Android 7.0头像剪裁上传功能Demo源码

程序员文章站 2021-12-25 10:56:44
...

之前也分享过上传头像的Demo、因为呢Android的升级问题、相信有很多哥们已经发现了、在Android 7.0的时候以前的上传头像的功能好像就不太好用了、现在呢给大家分享一个最新的上传头像的Demo、可以完美的兼容Android 7.0、上传头像兼容Android7.0、并且有显示圆形的剪切框、非常实用

兼容Android 7.0头像剪裁上传功能Demo源码


showSheetDialog方法

@SuppressWarnings("deprecation")
private void showSheetDialog() {
    View view = getLayoutInflater().inflate(
            R.layout.sp_photo_choose_dialog, null);

    final Dialog dialog = new Dialog(this, R.style.transparentFrameWindowStyle);
    dialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    Window window = dialog.getWindow();
    window.setWindowAnimations(R.style.main_menu_animstyle);
    WindowManager.LayoutParams wl = window.getAttributes();
    wl.x = 0;
    wl.y = getWindowManager().getDefaultDisplay().getHeight();
    wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
    wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    dialog.onWindowAttributesChanged(wl);
    dialog.setCanceledOnTouchOutside(true);
    dialog.show();
    Button btnCamera = (Button) view.findViewById(R.id.btn_to_camera);
    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 222);
                return;
            } else if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 222);
                return;
            } else if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 222);
                return;
            } else {
                Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SDPathUtils.getCachePath(), "temp.jpg")));
                    startActivityForResult(openCameraIntent, 2);
                } else {
                    Uri imageUri = FileProvider.getUriForFile(MainActivity.this, "com.camera_photos.fileprovider", new File(SDPathUtils.getCachePath(), "temp.jpg"));
                    openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(openCameraIntent, 2);
                }
            }
        }
    });
    Button btnPhoto = (Button) view.findViewById(R.id.btn_to_photo);
    btnPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            Intent intent = new Intent(Intent.ACTION_PICK, null);
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(intent, 1);
        }
    });
    Button btnCancel = (Button) view.findViewById(R.id.btn_to_cancel);
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
}


保存裁剪之后的图片数据
/**
 * 裁剪图片方法实现
 *
 * @param uri
 */
public void startPhotoZoom(Uri uri) {
    Intent intent = new Intent(getActivity(), PreviewActivity.class);
    intent.setDataAndType(uri, "image/*");
    startActivityForResult(intent, 3);
}

/**
 * 保存裁剪之后的图片数据
 *
 * @param picdata
 */
private void setPicToView(Intent picdata) {
    Bitmap bitmap = null;
    byte[] bis = picdata.getByteArrayExtra("bitmap");
    bitmap = BitmapFactory.decodeByteArray(bis, 0, bis.length);
    localImg = System.currentTimeMillis()   ".JPEG";

    if (bitmap != null) {

        SDPathUtils.saveBitmap(bitmap, localImg);
        Log.e("本地图片绑定", SDPathUtils.getCachePath()   localImg);
        setImageUrl(ivHeadLogo, "file:/"   SDPathUtils.getCachePath()   localImg, R.mipmap.head_logo);
    }
}

private DisplayImageOptions options;

public void setImageUrl(ImageView ivId, String imageUrl, int emptyImgId) {
    if (options == null) {
//            options = new DisplayImageOptions.Builder()
//                    .showImageOnLoading(emptyImgId)
//                    .showImageForEmptyUri(emptyImgId)
//                    .showImageOnFail(emptyImgId).cacheInMemory(true)
//                    .cacheOnDisk(true).considerExifParams(true)
//                    .bitmapConfig(Bitmap.Config.RGB_565)
//                    .displayer(new RoundedBitmapDisplayer(RoundNum)).build();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(emptyImgId)
                .showImageForEmptyUri(emptyImgId)
                .showImageOnFail(emptyImgId).cacheInMemory(true)
                .cacheOnDisk(true).considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
    }
    ImageLoader.getInstance().displayImage(imageUrl, ivId, options);
}


兼容Android 7.0头像上传源代码下载链接: android 7.0头像上传 密码: fzv6