OnResume和OnPause连续运行

时间:2018-08-27 13:28:26

标签: java android

我已经实现了从相机和画廊获取图像的代码,但是在允许访问设备上的照片,媒体和文件权限之后。我检测到了屏幕覆盖。我已关闭所有系统应用程序和其他应用程序的绘制功能,但仍会不断出现。并在恢复和暂停循环上连续运行。 这是我的代码,请帮忙。

这是我的权限代码

 if{ (!PermissionsAndroid.getInstance().checkWriteExternalStoragePermission(ServiceDetail_activity.this)) {
            PermissionsAndroid.getInstance().requestForWriteExternalStoragePermission((ServiceDetail_activity.this));
        } else {
            ImageUtils.getInstance().openImageChooser(ServiceDetail_activity.this, ServiceDetail_activity.this);
        } 

//This is my function to choose the image by camera or gallery
 public void openImageChooser(Context context, Activity activity) {
        List<Intent> cameraIntents = new ArrayList<>();
        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for (ResolveInfo res : listCam) {
            String packageName = res.activityInfo.packageName;
            Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri(context));
            cameraIntents.add(intent);
        }

        Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        galleryIntent.setType("image/*");

        Intent chooserIntent = Intent.createChooser(galleryIntent, "Choose");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
        activity.startActivityForResult(chooserIntent, IMAGE_CHOOSER_REQUEST_CODE);
    } 

这是我的onActivityResult代码

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {

            ImageUtils.getInstance().resampleImage(this, this, ImageUtils.getInstance().getCroppedImagePath(this));
        } else if (requestCode == Crop.REQUEST_CROP && resultCode == Crop.RESULT_ERROR) {
            // Handle the error got during cropping.
        } else if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_CANCELED) {
            // Cropping canceled by user.
        }
        else if (requestCode == ImageUtils.IMAGE_CHOOSER_REQUEST_CODE &&resultCode== Activity.RESULT_OK) {
            final boolean isCamera;
            if (data == null || data.getData() == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }
            if (isCamera) {
                selectedImagePath = ImageUtils.getInstance().getImagePath(this);
            } else {
                selectedImagePath = FileUtils.getPath(this, data.getData());
            }
            if ((selectedImagePath != null) && !(selectedImagePath.equals(""))) {

                ImageUtils.getInstance().beginCrop(this, this, selectedImagePath, false);

            }
        }

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

    }
This my manifest code.
 <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我在搜索上做了很多事情,但是不明白。

请向我推荐我错了的地方。我整日迷失了。

我已经在棉花糖设备6.0中对此进行了测试。如果有任何好友建议给我,请找出这个问题。这是我项目中非常重要的部分。感谢提前。

0 个答案:

没有答案