Java tutorial
package com.business.rushour.businessapp; import android.content.ActivityNotFoundException; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.design.widget.NavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AlertDialog; 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.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.Toast; import com.pkmmte.view.CircularImageView; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class RiderMainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { CircularImageView uploadphoto; final int PIC_CROP = 2; private Bitmap bitmap; private int PICK_IMAGE_REQUEST = 1; private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; public static final int MEDIA_TYPE_IMAGE = 1; private static int RESULT_LOAD_IMAGE = 1; // directory name to store captured images and videos private static final String IMAGE_DIRECTORY_NAME = "Download"; private Uri fileUri; // file url to store image/video @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ridermain); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); uploadphoto = (CircularImageView) findViewById(R.id.CircularImageViews); 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); View header = navigationView.getHeaderView(0); header.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { captureImage(); } }); RiderMainFragmentClass fragment = new RiderMainFragmentClass(); android.support.v4.app.FragmentTransaction fm = getSupportFragmentManager().beginTransaction(); fm.replace(R.id.content_frame, fragment); fm.commit(); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. displaySelectedScreen(item.getItemId()); return true; } private void displaySelectedScreen(int itemId) { //creating fragment object Fragment fragment = null; //initializing the fragment object which is selected switch (itemId) { case R.id.postride: postride(); break; case R.id.trackride: Intent login = new Intent(this, NavigationTrackRideActivity.class); startActivity(login); break; case R.id.history: Intent history = new Intent(this, NavigationHistoryActivity.class); startActivity(history); break; case R.id.wallet: Intent wallet = new Intent(this, NavigationWalletActivity.class); startActivity(wallet); break; case R.id.profile: Intent profile = new Intent(this, NavigationProfileActivity.class); startActivity(profile); break; case R.id.aboutus: Intent aboutus = new Intent(this, NavigationAboutUsActivity.class); startActivity(aboutus); break; case R.id.contactus: Intent contactus = new Intent(this, NavigationContactUsActivity.class); startActivity(contactus); break; case R.id.tc: Intent tc = new Intent(this, NavigationTermsConditionsActivity.class); startActivity(tc); break; case R.id.logout: Intent logout = new Intent(this, NavigationLogoutActivity.class); startActivity(logout); break; } //replacing the fragment if (fragment != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content_frame, fragment); ft.commit(); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); } public void postride() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); // Setting Dialog Title alertDialog.setTitle("Post Ride"); // Setting Dialog Message alertDialog.setMessage( "To Post Ride, you should be authenticated with your Driving License and Vehicle RC Information."); // Setting Icon to Dialog //alertDialog.setIcon(R.drawable.delete); // Setting Positive "Yes" Button alertDialog.setPositiveButton("Authenticate", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent aboutus = new Intent(getApplicationContext(), RiderRegistrationAsOwnerActivity.class); startActivity(aboutus); } }); // Setting Negative "NO" Button alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Write your code here to invoke NO event dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } private void captureImage() { final CharSequence[] options = { "Take Photo", "Choose from Gallery", "Cancel" }; AlertDialog.Builder builder = new AlertDialog.Builder(RiderMainActivity.this); builder.setTitle("Add Photo!"); builder.setItems(options, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { if (options[item].equals("Take Photo")) { try { final Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); //startActivityForResult(cameraIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // start the image capture Intent startActivityForResult(cameraIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); } catch (ActivityNotFoundException anfe) { String errorMessage = "Whoops - your device doesn't support capturing images!"; Toast toast = Toast.makeText(RiderMainActivity.this, errorMessage, Toast.LENGTH_SHORT); toast.show(); } } else if (options[item].equals("Choose from Gallery")) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, PICK_IMAGE_REQUEST); } else if (options[item].equals("Cancel")) { dialog.dismiss(); } } }); builder.show(); } /** * Here we store the file url as it will be null after returning from camera * app */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // save file url in bundle as it will be null on scren orientation // changes outState.putParcelable("file_uri", fileUri); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // get the file url fileUri = savedInstanceState.getParcelable("file_uri"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // if the result is capturing Image if (requestCode == PICK_IMAGE_REQUEST) { if (resultCode == RESULT_OK) { fileUri = data.getData(); // successfully captured the image // display it in image view previewCapturedImage(); } } else if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { if (resultCode == RESULT_OK) { previewCapturedImage(); } } else if (requestCode == PIC_CROP) { // user cancelled Image capture Bundle extras = data.getExtras(); //get the cropped bitmap Bitmap thePic = extras.getParcelable("data"); //ImageView imageView = (ImageView) findViewById(R.id.fullimageview); CircularImageView uploadphoto = (CircularImageView) findViewById(R.id.CircularImageViews); uploadphoto.setImageBitmap(thePic); } } /** * Display image from a path to ImageView */ private void previewCapturedImage() { try { // hide video preview Intent cropIntent = new Intent("com.android.camera.action.CROP"); //indicate image type and Uri cropIntent.setDataAndType(fileUri, "image/*"); //set crop properties cropIntent.putExtra("crop", "true"); //indicate aspect of desired crop cropIntent.putExtra("aspectX", 1); cropIntent.putExtra("aspectY", 1); //indicate output X and Y cropIntent.putExtra("outputX", 256); cropIntent.putExtra("outputY", 256); //retrieve data on return cropIntent.putExtra("return-data", true); //start the activity - we handle returning in onActivityResult startActivityForResult(cropIntent, PIC_CROP); } catch (NullPointerException e) { e.printStackTrace(); } } public Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); } private static File getOutputMediaFile(int type) { // External sdcard location File mediaStorageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME); // Create the storage directory if it does not exist if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory"); return null; } } // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); } else { return null; } return mediaFile; } }