简体   繁体   中英

how to use onActivityResult( ) in fragment

this code must return cropped image in imageView in fragment i am using cropper image to crop image and then put it in imageView its work in activity bit not work in fragment

Package com.example.rami_.esmatsongs;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import static android.app.Activity.RESULT_OK;
/**
 * A simple {@link Fragment} subclass.
 */
public class AccountFragment extends Fragment {
    private ImageView profile_picture;
    private TextView edit_profile_picture;
    public AccountFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_account, container, false);
        profile_picture = (ImageView) view.findViewById(R.id.profilePicture);
        profile_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cropImage();
            }
        });
        edit_profile_picture = (TextView) view.findViewById(R.id.changeProfilePic);
        edit_profile_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cropImage();
            }
        });
        return view;
    }

    public void cropImage() {
        CropImage.activity()
                 .setGuidelines(CropImageView.Guidelines.ON)
                 .setMinCropResultSize(512, 512)
                 .setAspectRatio(1, 1)
                 .getIntent(getContext());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                Uri postImageUri = result.getUri();
                profile_picture.setImageURI(postImageUri);
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        }
    }
}

mainActivity code

package com.example.rami_.esmatsongs;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.theartofdev.edmodo.cropper.CropImage;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView mainbottomNav;
    private HomeFragment homeFragment;
    private NotificationFragment notificationFragment;
    private AccountFragment accountFragment;
    private AboutFragment aboutFragment;
    private Uri mCropImageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // FRAGMENTS
        homeFragment = new HomeFragment();
        notificationFragment = new NotificationFragment();
        accountFragment = new AccountFragment();
        aboutFragment = new AboutFragment();
        initializeFragment();
        mainbottomNav = (BottomNavigationView) findViewById(R.id.mainBottomNav);
        mainbottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.home_bottom:
                        replaceFragment(homeFragment);
                        return true;

                    case R.id.account_bottom:
                        replaceFragment(accountFragment);
                        return true;

                    case R.id.notification_bottom:
                        replaceFragment(notificationFragment);
                        return true;

                    case R.id.about_bottom:
                        replaceFragment(aboutFragment);
                        return true;

                    default:
                        return false;
                }
            }
        });
    }

    private void initializeFragment() {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        fragmentTransaction.add(R.id.main_container, homeFragment);
        fragmentTransaction.add(R.id.main_container, notificationFragment);
        fragmentTransaction.add(R.id.main_container, accountFragment);
        fragmentTransaction.add(R.id.main_container, aboutFragment);
        fragmentTransaction.hide(notificationFragment);
        fragmentTransaction.hide(accountFragment);
        fragmentTransaction.hide(aboutFragment);

        fragmentTransaction.commit();

    }


    private void replaceFragment(Fragment fragment) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (fragment == homeFragment) {
            fragmentTransaction.hide(notificationFragment);
            fragmentTransaction.hide(accountFragment);
            fragmentTransaction.hide(aboutFragment);
        }

        if (fragment == accountFragment) {
            fragmentTransaction.hide(homeFragment);
            fragmentTransaction.hide(notificationFragment);
            fragmentTransaction.hide(aboutFragment);
        }

        if (fragment == notificationFragment) {
            fragmentTransaction.hide(homeFragment);
            fragmentTransaction.hide(accountFragment);
            fragmentTransaction.hide(aboutFragment);
        }

        if (fragment == aboutFragment) {
            fragmentTransaction.hide(homeFragment);
            fragmentTransaction.hide(accountFragment);
            fragmentTransaction.hide(notificationFragment);
        }

        fragmentTransaction.show(fragment);
        //fragmentTransaction.replace(R.id.main_container, fragment);
        fragmentTransaction.commit();
    }
}

    enter code here

Change

 public void cropImage()
    {
        CropImage.activity()
                .setGuidelines(CropImageView.Guidelines.ON)
                .setMinCropResultSize(512, 512)
                .setAspectRatio(1, 1)
                .getIntent(getContext());
    }

to

 public void cropImage()
    {
        CropImage.activity()
                .setGuidelines(CropImageView.Guidelines.ON)
                .setMinCropResultSize(512, 512)
                .setAspectRatio(1, 1)
                .start(getContext(), this);
    }

So the results will be delivered to the fragment and not to the activity

From Android Image Cropper repository

You can start the CropImageActivity from the fragment and receive the result in the fragment onActivityResult method. Using AppCompat fragment is the best option that allows you to use direct start method:

UPDATE:

Watch this issue in their Git Repository, suggesting another option,

you can get the intent and start the activity manually:

Intent intent = CropImage.activity(imageUri)
   .getIntent(getContext());
startActivityForResult(intent,CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);

Overide onActivityResult in Activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    FragmentManager supportFragmentManager = getSupportFragmentManager();
    Fragment fragmentById = supportFragmentManager.findFragmentById(R.id.container);
    if (fragmentById != null) {
        fragmentById.onActivityResult(requestCode, resultCode, data);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM