title: Get image name from selected image in gallery
body: <p>At the moment I am selecting an image from gallery and the whole path is shown example (/storage/emulated.0/DCIM/Camera/123.jpg) in the textview whereas I only want the name of the image to be shown example 123.jpg.</p>
  
  <pre><code>public String getRealPathFromURI(Uri uri) {
      String[] projection = { MediaStore.Images.Media.DATA };
      @SuppressWarnings("deprecation")
      Cursor cursor = managedQuery(uri, projection, null, null, null);
      int column_index = cursor
              .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      BitmapFactory.Options options;
      if (resultCode == RESULT_OK &amp;&amp; requestCode == PICTURE_SELECTED) {
          try {
              options = new BitmapFactory.Options();
              options.inSampleSize = 4;
              Uri selectedImage = data.getData();
              String s= getRealPathFromURI(selectedImage);
              imageName.append(s);
              Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
              String[] filePath = {MediaStore.Images.Media.DATA};
  
              Cursor cursor = getContentResolver().query(selectedImage, filePath, null, null, null);
              cursor.moveToFirst();
              String mImagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
  
              InputStream stream = getContentResolver().openInputStream(selectedImage);
              Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
              stream.close();
              //---orientation---
              try {
                  int rotate = 0;
                  try {
                      ExifInterface exif = new ExifInterface(
                              mImagePath);
                      dateTaken.append(exif.getAttribute(ExifInterface.TAG_DATETIME));
                      int orientation = exif.getAttributeInt(
                              ExifInterface.TAG_ORIENTATION,
                              ExifInterface.ORIENTATION_NORMAL);
  
                      switch (orientation) {
                          case ExifInterface.ORIENTATION_ROTATE_270:
                              rotate = 270;
                              break;
                          case ExifInterface.ORIENTATION_ROTATE_180:
                              rotate = 180;
                              break;
                          case ExifInterface.ORIENTATION_ROTATE_90:
                              rotate = 90;
                              break;
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  Matrix matrix = new Matrix();
                  matrix.postRotate(rotate);
                  yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true);
  
              }
              catch (Exception e) {}
              //---end of orientation---
  
              imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
              imgButton.setImageBitmap(yourSelectedImage);
          } catch (Exception e) {
              Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
          }
      } else {
          Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
      }
      Log.i(TAG, "onActivityResult - Portal");
  }
  </code></pre>
  
  <p>Followed this link <a href="https://stackoverflow.com/questions/20324155/get-filepath-and-filename-of-selected-gallery-image-in-android">Get filepath and filename of selected gallery image in Android</a></p>