0

您好,我正在尝试从 android 相机中裁剪图像。I 相机已激活,并且裁剪图像的活动也已激活。

但是,我无法将裁剪后的图像传递给另一个活动。

我搜索了,我找不到解决方案。我在做什么错?也许,是因为putExtras?

库页面: https ://github.com/ArthurHub/Android-Image-Cropper

我的 PhotoFragment.java


public class PhotoFragment extends Fragment{

    private static final String TAG = "PhotoFragment";

    //constants
    private static final int PHOTO_FRAGMENT_NUM = 1;
    private static final int GALLERY_FRAGMENT_NUM = 2;
    private static final int CAMERA_REQUEST_CODE = 5;


    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo, container, false);
        Log.d(TAG, "onCreateView: Started.");

        Button btnLaunchCamera = (Button) view.findViewById(R.id.btnLaunchCamera);
        btnLaunchCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: launching the camera.");



                // Create intent to Open Image
                Intent galleryIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(galleryIntent, CAMERA_REQUEST_CODE);
            }
        });


        return view;
    }


    private boolean isRootTask(){
        if(((ShareActivity)getActivity()).getTask() == 0){
            return true;
        }else{
            return false;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        Log.d("APP_DEBUG", String.valueOf(requestCode));

        try {
            // When an Image is picked
            if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();

                CropImage.activity(selectedImage)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .setMinCropResultSize(800, 800)
                        .setMaxCropResultSize(1000, 1000)
                        .start(getContext(), this);
            }
            // when image is cropped
            else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                Log.d("APP_DEBUG",result.toString());
                if (resultCode == Activity.RESULT_OK) {
                    Uri resultUri = result.getUri();
                    Log.d("APP_DEBUG",resultUri.toString());
                    Bitmap bitmap =  MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), resultUri);
                    //pofilePic.setImageBitmap(bitmap);

                    Toast.makeText(getContext(), "sssss", Toast.LENGTH_SHORT).show();


                    if(isRootTask()) {
                        try {
                            Log.d(TAG, "onActivityResult: received new bitmap from camera" + bitmap);
                            Intent intent = new Intent(getContext(), NextActivity.class);
                            intent.putExtra(getString(R.string.selected_bitmap), bitmap);
                            startActivity(intent);
                        }catch (NullPointerException e){
                            Log.d(TAG, "onActivityResult: NullPointerException" + e.getMessage());
                        }
                    }else {
                        try {
                            Log.d(TAG, "onActivityResult: received new bitmap from camera" + bitmap);
                            Intent intent = new Intent(getContext(), AccountSettingsActivity.class);
                            intent.putExtra(getString(R.string.selected_bitmap), bitmap);
                            intent.putExtra(getString(R.string.return_to_fragment), getString(R.string.edit_profile_fragment));
                            startActivity(intent);
                            getActivity().finish();
                        }catch (NullPointerException e){
                            Log.d(TAG, "onActivityResult: NullPointerException" + e.getMessage());
                        }

                    }



                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }
            }
            else {
                Toast.makeText(getActivity(), "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(getActivity(), "Something went wrong"+e.getMessage(), Toast.LENGTH_LONG)
                    .show();
        }

    }

我的 NextActivity.java 是:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        mFirebaseMethods = new FirebaseMethods(NextActivity.this);
        mcaption = (EditText) findViewById(R.id.caption);

        setupFirebaseAuth();

        ImageView backArrow = (ImageView) findViewById(R.id.ivBackArrow);
        backArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: closing the activity.");
                finish();
            }
        });

        TextView share = (TextView) findViewById(R.id.tvShare);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: navigating to the final share screen.");

                // upload the image to firebase
                Toast.makeText(NextActivity.this,"Attempting to upload a new photo", Toast.LENGTH_SHORT).show();
                String caption = mcaption.getText().toString();

                // check if the intent came from gallery or from the camera
                if(intent.hasExtra(getString(R.string.selected_image))){
                    imgUrl = intent.getStringExtra(getString(R.string.selected_image));
                    mFirebaseMethods.uploadNewPhoto(getString(R.string.new_photo), caption, imageCount, imgUrl, null);
                }
                else if(intent.hasExtra(getString(R.string.selected_bitmap))){
                    bitmap = (Bitmap) intent.getParcelableExtra(getString(R.string.selected_bitmap));
                    mFirebaseMethods.uploadNewPhoto(getString(R.string.new_photo), caption, imageCount, null, bitmap);

                }



            }
        });

        setImage();
    }


    /**
     * gets the image url from the incoming intent and displays the chosen image
     */

    private void setImage(){
        intent = getIntent();
        ImageView image = (ImageView) findViewById(R.id.imageShare);

        // check if the intent came from gallery or from the camera
        if(intent.hasExtra(getString(R.string.selected_image))){
            imgUrl = intent.getStringExtra(getString(R.string.selected_image));
            Log.d(TAG, "setImage: got new image url: " + imgUrl);
            // UniversalImageLoader handles null , oder wise we should check is image is null
            UniversalImageLoader.setImage(imgUrl, image, null, mAppend);
        }
        else if(intent.hasExtra(getString(R.string.selected_bitmap))){
            bitmap = (Bitmap) intent.getParcelableExtra(getString(R.string.selected_bitmap));
            Log.d(TAG, "setImage: got new Bitmap");
            image.setImageBitmap(bitmap);
        }
    }

我希望在 NextActivity.java 的 ImageView 图像中获得裁剪后的图像

mBitmap 为空??

4

0 回答 0