content: package com.ahmedbadereldin.videotrimmer;
  
  import android.content.ContentResolver;
  import android.content.Context;
  import android.database.Cursor;
  import android.net.Uri;
  import android.provider.OpenableColumns;
  import android.text.TextUtils;
  import android.util.Log;
  import android.webkit.MimeTypeMap;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  public class FileUtil {
      private static final int EOF = -1;
      private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
  
      // By default, Android doesn't provide support for JSON
      public static final String MIME_TYPE_JSON = "application/json";
  
      private FileUtil() {
  
      }
  
      public static File from(Context context, Uri uri) throws IOException {
          InputStream inputStream = context.getContentResolver().openInputStream(uri);
          String fileName = getFileName(context, uri);
          String[] splitName = splitFileName(fileName);
          File tempFile = File.createTempFile(splitName[0], splitName[1]);
          tempFile = rename(tempFile, fileName);
          tempFile.deleteOnExit();
          FileOutputStream out = null;
          try {
              out = new FileOutputStream(tempFile);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          if (inputStream != null) {
              copy(inputStream, out);
              inputStream.close();
          }
  
          if (out != null) {
              out.close();
          }
          return tempFile;
      }
  
      private static String[] splitFileName(String fileName) {
          String name = fileName;
          String extension = "";
          int i = fileName.lastIndexOf(".");
          if (i != -1) {
              name = fileName.substring(0, i);
              extension = fileName.substring(i);
          }
  
          return new String[]{name, extension};
      }
  
      private static String getFileName(Context context, Uri uri) {
          String result = null;
          if (uri.getScheme().equals("content")) {
              Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
              try {
                  if (cursor != null && cursor.moveToFirst()) {
                      result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  if (cursor != null) {
                      cursor.close();
                  }
              }
          }
          if (result == null) {
              result = uri.getPath();
              int cut = result.lastIndexOf(File.separator);
              if (cut != -1) {
                  result = result.substring(cut + 1);
              }
          }
          return result;
      }
  
      private static File rename(File file, String newName) {
          File newFile = new File(file.getParent(), newName);
          if (!newFile.equals(file)) {
              if (newFile.exists() && newFile.delete()) {
                  Log.d("FileUtil", "Delete old " + newName + " file");
              }
              if (file.renameTo(newFile)) {
                  Log.d("FileUtil", "Rename file to " + newName);
              }
          }
          return newFile;
      }
  
      private static long copy(InputStream input, OutputStream output) throws IOException {
          long count = 0;
          int n;
          byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
          while (EOF != (n = input.read(buffer))) {
              output.write(buffer, 0, n);
              count += n;
          }
          return count;
      }
  
      @Nullable
      public static String getMimeType(@NonNull Context context, @NonNull Uri uri) {
  
          String mimeType = null;
          if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
              ContentResolver cr = context.getContentResolver();
              mimeType = cr.getType(uri);
          } else {
              String fileExtension = getExtension(uri.toString());
  
              if(fileExtension == null){
                  return null;
              }
  
              mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                      fileExtension.toLowerCase());
  
              if(mimeType == null){
                  // Handle the misc file extensions
                  return handleMiscFileExtensions(fileExtension);
              }
          }
          return mimeType;
      }
  
      @Nullable
      private static String getExtension(@Nullable String fileName){
  
          if(fileName == null || TextUtils.isEmpty(fileName)){
              return null;
          }
  
          char[] arrayOfFilename = fileName.toCharArray();
          for(int i = arrayOfFilename.length-1; i > 0; i--){
              if(arrayOfFilename[i] == '.'){
                  return fileName.substring(i+1, fileName.length());
              }
          }
          return null;
      }
  
      @Nullable
      private static String handleMiscFileExtensions(@NonNull String extension){
  
          if(extension.equals("json")){
              return MIME_TYPE_JSON;
          }
          else{
              return null;
          }
      }
  
      public static String getMimeType(File file) {
          String mimeType = "";
          String extension = getExtention(file.getName());
          if (MimeTypeMap.getSingleton().hasExtension(extension)) {
              mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
          }
          return mimeType;
      }
  
      private static String getExtention(String fileName){
          char[] arrayOfFilename = fileName.toCharArray();
          for(int i = arrayOfFilename.length-1; i > 0; i--){
              if(arrayOfFilename[i] == '.'){
                  return fileName.substring(i+1, fileName.length());
              }
          }
          return "";
      }
  }