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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从图库中选择照片(onActivityResult()问题 - 我想是这样) - 片段Android相关的知识,希望对你有一定的参考价值。

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

下面是我的AddFragment.java代码,我从中选择photo和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);

    }

}

}

main activity.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);
 }

logcat中没有任何内容。我可以看到onActivityResult()方法没有得到调用

答案

你必须经常在你的Activity的super.onActivityResult()中调用onActivityResult。这就是将onActivityResult回调调度到使用正确的requestCodes调用startActivityForResult的Fragments - 您手动调度它的方法不会重新映射请求代码:

// 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以准确捕捉这个问题,并计划在碎片1.1.0-alpha06中发布。

以上是关于从图库中选择照片(onActivityResult()问题 - 我想是这样) - 片段Android的主要内容,如果未能解决你的问题,请参考以下文章

onActivityResult() 没有在 DialogFragment 中执行

如果从图库中选择图像或选择视频,则在 onActivityResult 中识别 - Android

onActivityResult 不会从 viewPager 片段调用

从画廊返回后,不在 Fragment 中调用 onActivityResult

使用片段从图库中选择图像

使用 Phonegap/Cordova 相机插件从相机或图库中选择照片