从图库中选择照片(onActivityResult()问题-我想是这样)-Fragment Android

时间:2019-03-30 23:46:04

标签: android fragment photo

我有很大的问题。我试图从画廊view from app中选择照片,但是当我单击照片时,我的应用程序会最小化。当我最大化它时,应用程序处于启动状态。我看到onActivityResult()方法没有调用。

下面是我的AddFragment.java代码,我从中选择照片和MainAcitivity。我检查了所有论坛和网站,没有人遇到这样的问题。谢谢您的建议:)我不知道该怎么办:(

public class AddFragment extends Fragment{


private Button addPhoto;
private ImageView imageView;
private Uri imageUri;
private final int PICK_IMAGE_REQUEST = 1;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_add,container,false);


    imageView=v.findViewById(R.id.imgView);
    addPhoto=v.findViewById(R.id.button_addphoto);
    addPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openFileChooser();

        }
    });


    return v;
}


private void openFileChooser(){
    Intent intent=new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    Log.d(TAG, "OPENFILECHOOSER");
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE_REQUEST);
    Log.d(TAG, "OPENFILECHOOSER2");

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "OPENFILECHOOSER3");

    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            &&data != null && data.getData() !=null){
        imageUri = data.getData();

        Picasso.get().load(imageUri).resize(50,50).into(imageView);

    }

}

}

MainActivity.java

public class MainActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout drawer;
NavigationView navigationView;
private FragmentManager fragmentManager;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar toolbar=findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawer_layout);
    navigationView=findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ActionBarDrawerToggle toogle=new ActionBarDrawerToggle(this,drawer,toolbar,
            R.string.navigation_drawer_open,R.string.navigation_drawer_close);
    drawer.addDrawerListener(toogle);
    toogle.syncState();
    if(savedInstanceState==null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new HomeFragment()).commit();
        navigationView.setCheckedItem(R.id.nav_home);
    }
   fragmentManager = getFragmentManager();


}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch(item.getItemId()){

        case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();

            break;

        case R.id.nav_add_new:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new AddFragment()).commit();


            break;

        case R.id.nav_login:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new LoginFragment()).commit();


            break;


        case R.id.nav_browse:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new BrowseFragment()).commit();


            break;


        case R.id.nav_logout:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();

            signOut(FirebaseAuth.getInstance());
            break;

        case R.id.nav_profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();

            break;
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


@Override
public void onBackPressed() {
    if(drawer.isDrawerOpen(GravityCompat.START)){
        drawer.closeDrawer(GravityCompat.START);

    }
    else if (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack();
    } else {
        //super.onBackPressed();
    }

}

private void signOut(FirebaseAuth mAuth) {
    if (mAuth != null) {
        mAuth.signOut();
        navigationView.getMenu().findItem(R.id.nav_login).setVisible(true);
        navigationView.getMenu().findItem(R.id.nav_logout).setVisible(false);
        navigationView.getMenu().findItem(R.id.nav_profile).setVisible(false);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "OPENFILECHOOSER4");
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    fragment.onActivityResult(requestCode,resultCode,data);



}

}

// In your MainActivity.java


@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Log.d(TAG, "OPENFILECHOOSER4");
     super.onActivityResult(requestCode, resultCode, data);
 }

After pick photo

logcat中什么也没有出现。我可以看到方法onActivityResult()没有被呼叫

1 个答案:

答案 0 :(得分:1)

您必须始终在活动的super.onActivityResult()中致电onActivityResult。这就是用正确的requestCodes向调用onActivityResult的Fragments发送startActivityForResult回调的原因-您手动分配它的方法不会重新映射请求代码:

// In your MainActivity.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "OPENFILECHOOSER4");
    super.onActivityResult(requestCode, resultCode, data);
}

This issue已提交以添加@CallSuper来完全解决此问题,该文件计划在Fragments 1.1.0-alpha06中发布。