【发布时间】:2019-12-12 23:40:23
【问题描述】:
所以我想在我的应用程序中添加相机意图..但是在我的应用程序中,当您单击“打开相机”按钮时,它会打开相机,但我希望相机在我单击照片选项卡时立即出现..请有人帮助我,我将非常感激。
标签
/**
* return the current tab number
* 0 = Gallery_Fragment
* 1 = Photo_Fragment
* @return
*/
public int getCurrentTabNumber(){
return mViewPager.getCurrentItem();
}
/**
* setup viewpager for manager the tabs
*/
private void setupViewPager(){
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new GalleryFragment());
adapter.addFragment(new PhotoFragment());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabsBottom);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setText(getString(R.string.gallery));
tabLayout.getTabAt(1).setText(getString(R.string.photo));
tabLayout.setTabTextColors(getResources().getColor(R.color.white), getResources().getColor(R.color.white));
}
照片片段
公共类 PhotoFragment 扩展片段 { private static final String TAG = "PhotoFragment";
//constant
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(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_photo, container, false);
Log.d(TAG, "onCreateView: started.");
Button btn = (Button) view.findViewById(R.id.btnLaunchCamera);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: Launching Camera");
if (((ShareActivity)getActivity()).getCurrentTabNumber()== PHOTO_FRAGMENT_NUM){
if (((ShareActivity)getActivity()).checkPermissions(Permissions.CAMERA_PERMISSIONS[0])){
Log.d(TAG, "onClick: starting camera");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}else{
Intent intent = new Intent (getActivity(), ShareActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE){
Log.d(TAG, "onActivityResult: done taking a photo.");
Log.d(TAG, "onActivityResult: attempting to navigate tofinal share screen");
//navigate to the final share screen to publish photo
}
}
}
【问题讨论】:
标签: android tabs camera fragment