兼容Android 7.0头像剪裁上传功能Demo源码
2017-02-07 13:27:30  By: shinyuu

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

android 7.0头像上传


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

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+