有关安卓7.0头像切换的天坑爬坑记录

好几天前我遇到这个问题,我花了好几天才修复了这个bug,特此记录。

小米,你懂我意思吧,所有的app都要为它做一次适配,不巧,我就是小米用户。。。。。

当我第一次看到这个bug情况的时候,它是这样的:当你点击切换切换头像时,它闪退了,其他的低版本机器都是好的,当我意识到这个问题的时候,我就已经发现事情不妙了,首先,我看到了这篇文章https://blog.csdn.net/eclothy/article/details/42719217

嗯。。。。有点意思,并没有解决问题,之后,虽然在debug之后我也发现了intent中没有值的问题,但是好像又有哪里不一样。

之后我又搜到了这篇文章https://blog.csdn.net/jdfkldjlkjdl/article/details/78599887?locationNum=10&fps=1(其实说实话,我看到的不是这一篇,因为找不到了。。。。这两篇差不多一个意思,要解决这个问题,复制粘贴是行不通的,你要明白你哪里有问题),看完上面这篇文章,我明白了,要加东西是吧,我全部照着加完,然后......还是不对,这咋整!!!!!!!!想了想,不对,坑定是哪有问题,intent里面还是啥也没有啊,啥啥聊天软件,启动!

有个老哥给我这样解释道,你懂我意思吧。

然后最关键的一步来了,谷歌源码有瑕疵,但是,我可以用开源框架呀!!机智

Ucrop解决问题。

关键代码如下:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==GALLERY_REQUSET_CODE) {
        handleGalleryResult(resultCode, data);
    }
    else if (requestCode==GALLERY_REQUSET_CODE_KITKAT) {
        handleGalleryKitKatResult(resultCode, data);
    }
    else if (resultCode == RESULT_OK) {
        //裁切成功
        if (requestCode == UCrop.REQUEST_CROP) {
            Uri croppedFileUri = UCrop.getOutput(data);
            //获取默认的下载目录
            String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
            String filename = String.format("%d_%s", Calendar.getInstance().getTimeInMillis(), croppedFileUri.getLastPathSegment());
            saveFile= new File(downloadsDirectoryPath, filename);
            //保存下载的图片
            FileInputStream inStream = null;
            FileOutputStream outStream = null;
            FileChannel inChannel = null;
            FileChannel outChannel = null;
            try {
                inStream = new FileInputStream(new File(croppedFileUri.getPath()));
                outStream = new FileOutputStream(saveFile);
                //Log.w("faceFile在哪呢","啊哈,在这里"+saveFile);
                inChannel = inStream.getChannel();
                outChannel = outStream.getChannel();
                inChannel.transferTo(0, inChannel.size(), outChannel);
                String ucrop = saveFile.getAbsolutePath();
                //Log.w("ucrop","啊哈,在这里"+ucrop);
                String url = "ucrop";
                //Log.w("ucrop","ucrop的值现在是什么呢"+ucrop);
                ImageView imageView = (ImageView) findViewById(R.id.imageView);
                Glide.with(this)
                        .asBitmap()
                        .load(url)
                        .into(imageView);//这里是图片加载框架
                //Log.w("url","是这个地方的图片被展示了出来"+url);
                savePic();
                // Toast.makeText(this, "裁切后的图片保存在:" + saveFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    outChannel.close();
                    outStream.close();
                    inChannel.close();
                    inStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //裁切失败
    if (resultCode == UCrop.RESULT_ERROR) {

    }
}

/**选择图片
 * @param resultCode
 * @param data
 */
private void handleGalleryResult(int resultCode, Intent data) {
    if (resultCode != RESULT_OK) return;
    String path = data.getData().getPath();
    Bitmap image = BitmapFactory.decodeFile(path);
    File faceFile;
    faceFile = ImageUtil.savePhotoToSDCard(image);
    Uri fileUri = Uri.fromFile(faceFile);
    routeToCrop(fileUri);
}

/**选择图片 7。0+
 * @param resultCode
 * @param data
 */
// Result uri is "content://" after Android 4.4
private void handleGalleryKitKatResult(int resultCode, Intent data) {
    if (resultCode != RESULT_OK) return;
    Uri contentUri = data.getData();
    if (contentUri == null) return;
    try {
        ParcelFileDescriptor parcelFileDescriptor =
                MyInfoActivity.this.getContentResolver().openFileDescriptor(contentUri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        faceFile = ImageUtil.savePhotoToSDCard(image);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    Uri fileUri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        // Android 7.0 "file://" uri权限适配
        fileUri = FileProvider.getUriForFile(MyInfoActivity.this,
                "gavinli.translator", faceFile);
    } else {
        fileUri = Uri.fromFile(faceFile);
    }
    routeToCrop(fileUri);
}



/**
 * 裁剪头像
 * @param uri
 */

private void routeToCrop(Uri uri) {

    Intent intent = new Intent(this,UCropActivity.class);
    Options options = new Options();
    intent.setDataAndType(uri, "image/*");
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
            | Intent.FLAG_GRANT_READ_URI_PERMISSION);//给当前URI赋予临时权限

    Uri destinationUri = Uri.fromFile(new File(getExternalCacheDir(), "uCrop.jpg"));  Log.w("uri","地址是"+uri);

    UCrop uCrop = of(uri, destinationUri);//第一个参数是裁剪前的uri,第二个参数是裁剪后的uri
    uCrop.withOptions(options);
    //下面参数分别是缩放,旋转,裁剪框的比例
    options.setAllowedGestures(UCropActivity.ALL,UCropActivity.NONE, UCropActivity.ALL);
    options.setToolbarTitle("头像剪裁");//设置标题栏文字
    options.setCropGridStrokeWidth(2);//设置裁剪网格线的宽度
    options.setCropFrameStrokeWidth(10);//设置裁剪框的宽度
    options.setMaxScaleMultiplier(3);//设置最大缩放比例
    options.setHideBottomControls(true);//隐藏下边控制栏
    options.setShowCropGrid(false);  //设置是否显示裁剪网格
    //options.//继续看源码。。。。。。。
    uCrop.withAspectRatio(9,9);
    options.setOvalDimmedLayer(true);//设置是否为圆形裁剪框
    options.setShowCropFrame(false); //设置是否显示裁剪边框(true为方形边框)
    uCrop.withMaxResultSize(400,400);
    options.setMaxBitmapSize(1200);//控制剪裁时的图片质量
    options.setToolbarWidgetColor(Color.parseColor("#ffffff"));//标题字的颜色以及按钮颜色
    options.setDimmedLayerColor(Color.parseColor("#AA000000"));//设置裁剪外颜色
    options.setToolbarColor(Color.parseColor("#000000")); // 设置标题栏颜色
    options.setStatusBarColor(Color.parseColor("#000000"));//设置状态栏颜色
    options.setCropGridColor(Color.parseColor("#ffffff"));//设置裁剪网格的颜色
    options.setCropFrameColor(Color.parseColor("#ffffff"));//设置裁剪框的颜色
    uCrop.withOptions(options);
    uCrop.start(this);
    Log.w("destinationUri","地址是"+destinationUri);

    startActivityForResult(intent, CROP_REQUEST_CODE);
}
问题解决。
ps:Intent intent = new Intent(this,UCropActivity.class);这句话可以解决另外一个bug:就是开始剪裁图片时会弹窗选程序的问题。

转载请注明出处,谢谢。

扫描二维码关注公众号,回复: 2337961 查看本文章

猜你喜欢

转载自blog.csdn.net/best_intheworld/article/details/81135453