无法捕获多个活动请求的结果

时间:2017-01-12 05:23:44

标签: android

我创建了一个具有此主题的活动

    Theme.AppCompat.Dialog

使用主题会导致一些问题,如果主题效果不好。

问题是在Java类中我启动了一个请求但是第二个的意图没有启动

   private void setupProfileImage() {
      startActivityForResult(getPickImageChooserIntent(this), PICTURE_INTENT);
   }

捕获结果

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_INTENT) {
        Uri imageUri = getPickImageResultUri(data, this);

        // For API >= 23 we need to check specifically that we have permissions to read external storage,
        // but we don't know if we need to for the URI so the simplest is to try open the stream and see if we get error.
        boolean requirePermissions = false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
                isUriRequiresPermissions(imageUri, this)) {

            // request permissions and handle the result in onRequestPermissionsResult()
            requirePermissions = true;
            mImageUri = imageUri;
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        }

        if (!requirePermissions) {
            startCropImageActivity(imageUri);
        }
    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            Picasso.with(this).load(resultUri).fit().centerCrop().into(profilePicture);
            profileChosen = true;
            mImageUri = resultUri;
        }
    }
}

第二意图

    private void startCropImageActivity(Uri imageUri) {
       if (imageUri == null){
            return;
        }
        CropImage.activity(imageUri)
                .setAspectRatio(1,1)
                .setFixAspectRatio(true)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAutoZoomEnabled(true)
                .setCropShape(CropImageView.CropShape.OVAL)
                .start(this);
    }

注意:当我从活动中删除主题时,它的工作非常精细

我真的想要一个对话而不是整页活动!

修改

    static Intent getPickImageChooserIntent(Activity activity) {

    // Determine Uri of camera image to  save.
    Uri outputFileUri = getCaptureImageOutputUri(activity);

    List<Intent> allIntents = new ArrayList<>();
    PackageManager packageManager = activity.getPackageManager();

    // collect all camera intents
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        if (outputFileUri != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        }
        allIntents.add(intent);
    }

    // collect all gallery intents
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
    for (ResolveInfo res : listGallery) {
        Intent intent = new Intent(galleryIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        allIntents.add(intent);
    }

    // the main intent is the last in the  list (fucking android) so pickup the useless one
    Intent mainIntent = allIntents.get(allIntents.size() - 1);
    for (Intent intent : allIntents) {
        if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
            mainIntent = intent;
            break;
        }
    }
    allIntents.remove(mainIntent);

    // Create a chooser from the main  intent
    Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");

    // Add all other intents
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));

    return chooserIntent;
}
/**
 * Get URI to image received from capture  by camera.
 */
private static Uri getCaptureImageOutputUri(Activity activity) {
    Uri outputFileUri = null;
    File getImage = activity.getExternalCacheDir();
    if (getImage != null) {
        outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg"));
    }
    return outputFileUri;
}

/**
 * Get the URI of the selected image from  { getPickImageChooserIntent()}.<br/>
 * Will return the correct URI for camera  and gallery image.
 *
 * @param data the returned data of the  activity result
 */
static Uri getPickImageResultUri(Intent data, Activity activity) {
    boolean isCamera = true;
    if (data != null && data.getData() != null) {
        String action = data.getAction();
        isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
    }
    return isCamera ? getCaptureImageOutputUri(activity) : data.getData();
}

/**
 * Test if we can open the given Android URI to test if permission required error is thrown.<br>
 */
static boolean isUriRequiresPermissions(Uri uri, Activity activity) {
    try {
        ContentResolver resolver = activity.getContentResolver();
        InputStream stream = resolver.openInputStream(uri);
        stream.close();
        return false;
    } catch (FileNotFoundException e) {
        if (e.getCause() instanceof ErrnoException) {
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "isUriRequiresPermissions: " + e.getMessage());
    }
    return false;
}

}

0 个答案:

没有答案