点击拍照后崩溃

来源:14-8 一键式相机,图片处理剪裁和动态权限封装-3

慕九州6385248

2018-10-30

我看这个回答是因为需要动态申请权限,但是我这里也是申请了,但是为什么还是报错呢

图片描述

权限申请的代码

package com.example.latte_core.delegates;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.example.latte_core.ui.camera.CameraImageBean;
import com.example.latte_core.ui.camera.LatteCamera;
import com.example.latte_core.ui.camera.RequestCodes;
import com.yalantis.ucrop.UCrop;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public abstract class PermissionCheckerDelegate extends BaseDelegate {

    //不是直接调用方法
    @NeedsPermission(Manifest.permission.CAMERA)
    void startCamera() {
        LatteCamera.start(this);
    }

    //这个是真正调用的方法
    public void startCameraWithCheck() {
        PermissionCheckerDelegatePermissionsDispatcher.startCameraWithPermissionCheck(this);
    }

    //扫描二维码(不直接调用)
    @NeedsPermission(Manifest.permission.CAMERA)
    void startScan(BaseDelegate delegate) {
       // delegate.getSupportDelegate().startForResult(new ScannerDelegate(), RequestCodes.SCAN);
    }

    public void startScanWithCheck(BaseDelegate delegate) {
        PermissionCheckerDelegatePermissionsDispatcher.startScanWithPermissionCheck(this, delegate);

    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    void onCameraDenied() {
        Toast.makeText(getContext(), "不允许拍照", Toast.LENGTH_LONG).show();
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    void onCameraNever() {
        Toast.makeText(getContext(), "永久拒绝权限", Toast.LENGTH_LONG).show();
    }

    @OnShowRationale(Manifest.permission.CAMERA)
    void onCameraRationale(PermissionRequest request) {
        showRationaleDialog(request);
    }

    private void showRationaleDialog(final PermissionRequest request) {
        if (getContext() != null) {
            new AlertDialog.Builder(getContext())
                    .setPositiveButton("同意使用", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            request.proceed();
                        }
                    })
                    .setNegativeButton("拒绝使用", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            request.cancel();
                        }
                    })
                    .setCancelable(false)
                    .setMessage("权限管理")
                    .show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionCheckerDelegatePermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case RequestCodes.TAKE_PHOTO:
                    final Uri resultUri = CameraImageBean.getInstance().getPath();
                    if (getContext() != null) {
                        UCrop.of(resultUri, resultUri)
                                .withMaxResultSize(400, 400)
                                .start(getContext(), this);
                    }
                    break;
                case RequestCodes.PICK_PHOTO:
                    if (data != null) {
                        final Uri pickPath = data.getData();
                        //从相册选择后需要有个路径存放剪裁过的图片
                        final String pickCropPath = LatteCamera.createCropFile().getPath();
                        if (pickPath != null && getContext() != null) {
                            UCrop.of(pickPath, Uri.parse(pickCropPath))
                                    .withMaxResultSize(400, 400)
                                    .start(getContext(), this);
                        }
                    }
                    break;
                case RequestCodes.CROP_PHOTO:
                    final Uri cropUri = UCrop.getOutput(data);
                    //拿到剪裁后的数据进行处理
//                    @SuppressWarnings("unchecked") final IGlobalCallback<Uri> callback = CallbackManager
//                            .getInstance()
//                            .getCallback(CallbackType.ON_CROP);
//                    if (callback != null) {
//                        callback.executeCallback(cropUri);
//                    }
                    break;
//                case RequestCodes.CROP_ERROR:
//                    Toast.makeText(getContext(), "剪裁出错", Toast.LENGTH_SHORT).show();
//                    break;
                default:
                    break;
            }
        }
    }
}

写回答

2回答

凌伊

2018-11-21

我也发现了这个问题,加入一个读写权限的申请就好了

@NeedsPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void checkStoryPermision() {
    LatteCamera.start(this);
}

@OnShowRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void onStorageRationale(final PermissionRequest request) {
    showRationaleDialog(request);
}

@OnPermissionDenied(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void onStorageDenied() {
    Toast.makeText(getContext(), "不给存储权限", Toast.LENGTH_LONG).show();
}

@OnNeverAskAgain(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void onStroageNever() {
    Toast.makeText(getContext(), "永久拒绝权限", Toast.LENGTH_LONG).show();
}

/**
 * 不是直接调用方法
 */
@NeedsPermission(Manifest.permission.CAMERA)
public void startCamera() {
    PermissionCheckerDelegatePermissionsDispatcher.checkStoryPermisionWithPermissionCheck(this);
}



0
1
慕九州6385248
非常感谢!
2019-08-07
共1条回复

傅猿猿

2018-10-31

这样,有些手机是需要申请文件写入权限的,照葫芦画瓢写一个文件的申请方法,方法体里面是空的就好,然后在打开照相机的fragment之前,调用一下这个withcheck方法,动态申请一次就好,保险起见以后都可以这样

0
0

Android通用框架设计与完整电商App开发

从第一行代码开始教你,通用且一键式配置的框架设计并轻松实现完整电商App

2028 学习 · 1182 问题

查看课程