PhotoActivity:
package com.example.zaki.mycamera; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import com.example.zaki.mycamera.FileWork.FileWork; import com.example.zaki.mycamera.SQLite.WorkSQLite; import java.io.File; import java.io.FileOutputStream; import static com.example.zaki.mycamera.TimeWork.DateTime.getTime; public class PhotoActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { SurfaceView surfaceView; Camera camera = null; MediaRecorder mediaRecorder; File pictures; Boolean cameraEnabled = false; private void getImageSurface() { surfaceView = (SurfaceView) findViewById(R.id.surfaceView); SurfaceHolder holder = surfaceView.getHolder(); holder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Hey", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } });*/ DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); //navigationView.setClickable(true); //navigationView.setContextClickable(true); pictures = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0); } else { cameraEnabled = true; } getImageSurface(); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == 0) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { cameraEnabled = true; } } } @Override protected void onResume() { super.onResume(); if (cameraEnabled) { camera = Camera.open(); } getImageSurface(); } @Override protected void onPause() { super.onPause(); if (cameraEnabled) { releaseMediaRecorder(); if (camera != null) camera.stopPreview(); camera.release(); camera = null; } surfaceView = null; } private void releaseMediaRecorder() { if (mediaRecorder != null) { mediaRecorder.reset(); mediaRecorder.release(); mediaRecorder = null; camera.lock(); } } public void onClickTakePhoto(View view) { if (cameraEnabled) { camera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { String fileName = getTime().replaceAll(" ", "") + ".JPG"; File file = new File(pictures.getAbsolutePath()+File.separator+fileName); if (!file.exists()) file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }); surfaceView = null; getImageSurface(); } } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.delete) { FileWork.deleteFiles(WorkSQLite.GetPoints(this)); return true; } else if (id == R.id.upload) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.PhotoCamera) { //android.app.FragmentManager fragmentManager = getFragmentManager(); //fragmentManager.beginTransaction().replace(R.id.content_frame, new ).commit(); Intent photo = new Intent(this, PhotoActivity.class); startActivity(photo); } else if (id == R.id.VideoCamera) { Intent video = new Intent(this, VideoActivity.class); startActivity(video); } else if (id == R.id.MyPhotos) { Intent myPhotos = new Intent(this, FileActivity.class); myPhotos.putExtra("dir", "pic"); startActivity(myPhotos); } else if (id == R.id.MyVideos) { Intent myVideos = new Intent(this, FileActivity.class); myVideos.putExtra("dir", "vid"); startActivity(myVideos); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } activity_photo.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> <include layout="@layout/photo"></include> <include layout="@layout/app_bar_main"></include> </android.support.v4.widget.DrawerLayout> photo.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="391dp" android:layout_marginTop="60dp"> </SurfaceView> <Button android:id="@+id/btnTakePicture" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/surfaceView" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:onClick="onClickTakePhoto" android:text="@string/take_picture" android:layout_alignParentEnd="true"> </Button> </LinearLayout> app_bar_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!--tools:context="com.example.zaki.mycamera.PhotoActivity"--> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> nav_header_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/camera_pic" android:gravity="bottom" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:theme="@style/ThemeOverlay.AppCompat.Dark" android:weightSum="1"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="MyCamera" android:textAppearance="@style/TextAppearance.AppCompat.Body1" android:textColor="@color/colorPrimaryDark" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="It's works (maybe)" android:textColorHighlight="@color/colorPrimary" /> </LinearLayout>
DrawerLayoutagain? You do thisonCreate. Still, if you want to make a side menu in all activities and smooth transitions, it is better to use theMaterial Drawer- Flippy libraryNavigationViewwill soon becomedeprecated. Nobody uses it! For the time being, I notice - all users use it - Flippy