Android Open Source - Exif-Editor Ega File System Base






From Project

Back to project page Exif-Editor.

License

The source code is released under:

MIT License

If you think the Android project Exif-Editor listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * @user https://github.com/Egatuts/* w  w  w .ja va 2 s.  c  om*/
 * @repo https://github.com/Egatuts/Exif-Editor.git
 * @file https://github.com/Egatuts/Exif-Editor/blob/master/src/git/egatuts/android/FileUtils/EgaFileSystemBase.java
 * @package git.egatuts.android.FileUtils
 * @license MIT (http://www.opensource.org/licenses/MIT)
 */
package git.egatuts.android.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.MimeTypeMap;

/**
 * Abstraction layer for file system management.
 * @author Esa Garca
 * @author EgaTuts
 * @version 1.0.0
 * @created 25/8/2014 at 16:11:35.
 */
public abstract class EgaFileSystemBase
{
  
  /**
   * Private Activity reference.
   */
  private static Activity myActivity;
  
  /**
   * Gets the active activity.
   * @return The active activity.
   */
  public abstract Activity getActivity ();
  
  /**
   * Copies files from one path to another.
   * @param from    Java File instance of the original file path.
   * @param to      Java File instance of the new file path.
   * @throws IOException Signals that an I/O exception has occurred.
   * @see File
   */
  public abstract void copyFile (File from, File to) throws IOException;
  
  /**
   * Copies files from one path to another.
   * @param from    Absolute path of the original file.
   * @param to      Absolute path of the new file location.
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public abstract void copyFile (String from, String to) throws IOException;
  
  /**
   * Deletes the given file or directory.
   * @param file    The file or directory to delete.
   * @throws IOException Signals that an I/O exception has occurred.
   * @see File
   */
  public abstract void deleteFile (File file) throws IOException;
  
  /**
   * Deletes the given file or directory.
   * @param file    The file or directory absolute path to delete.
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public abstract void deleteFile (String file) throws IOException;
  
  /**
   * Moves a file from one location to other.
   * @param from    The file or directory to move.
   * @param to      The new directory location.
   * @throws IOException Signals that an I/O exception has occurred.
   * @see File
   */
  public abstract void moveFile (File from, File to) throws IOException;
  
  /**
   * Moves a file from one location to other.
   * @param from    The file or directory absolute path to move.
   * @param to      The new directory absolute path.
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public abstract void moveFile (String from, String to) throws IOException;
  
  /**
   * Gets the application public files directory.
   * @param external    Flags if the location should be the external or the internal.
   * @return Public files location.
   */
  public File getApplicationPublicDir (boolean external)
  {
    myActivity = getActivity();
    if (external) return myActivity.getExternalFilesDir(null);
    return myActivity.getFilesDir();
  }
  
  /**
   * Gets the application external public directory.
   * @return The application external public directory File instance.
   * @see #getApplicationPublicDir(boolean)
   */
  public File getApplicationExternalPublicDir ()
  {
    return getApplicationPublicDir(true);
  }
  
  /**
   * Gets the application internal public directory.
   * @return The application internal public directory File instance.
   * @see #getApplicationPublicDir(boolean)
   */
  public File getApplicationInternalPublicDir ()
  {
    return getApplicationPublicDir(false);
  }
  
  /**
   * Gets the relative directory to the application public files directory.
   * @param path        The relative path.
   * @param create      Flags if should be created it's parent directories if doesn't exists.
   * @param external    Flags if the public files directory should be the external or the internal one.
   * @return The relative directory to the specified public files directory.
   */
  public File getRelativeDir (String path, boolean create, boolean external)
  {
    File dir = new File( getApplicationPublicDir(create), path );
    if ( !dir.exists() && create ) dir.mkdirs();
    return dir;
  }
  
  /**
   * Gets the relative directory without the create flag.
   * @param path        The relative path.
   * @param external    Flags if the public files directory should be the external or the internal one.
   * @return The relative directory to the specified public files directory.
   * @see #getRelativeDir(String, boolean, boolean)
   */
  public File getRelativeDir (String path, boolean external)
  {
    return getRelativeDir(path, true, external);
  }
  
  /**
   * Gets the relative directory without the external flag.
   * @param path      The relative path.
   * @param create    Flags if should be created it's parent directories if doesn't exists.
   * @return The relative path to the internal public files directory.
   * @see #getRelativeDir(String, boolean, boolean)
   */
  public File getInternalRelativeDir (String path, boolean create)
  {
    return getRelativeDir(path, create, false);
  }
  
  /**
   * Gets the internal relative directory without the create and external flags.
   * @param path    The relative path.
   * @return The relative path to the internal public files directory.
   * @see #getRelativeDir(String, boolean, boolean)
   */
  public File getInternalRelativeDir (String path)
  {
    return getRelativeDir(path, true, false);
  }
  
  /**
   * Gets the relative path to the external public files directory without the external flag.
   * @param path      The relative path.
   * @param create    Flags if should be created it's parent directories if doesn't exists.
   * @return The relative path to the external public files directory.
   */
  public File getExternalRelativeDir (String path, boolean create)
  {
    return getRelativeDir(path, create, true);
  }
  
  /**
   * Gets the relative path to the external public files directory without the create and external flag.
   * @param path      The relative path.
   * @param create    Flags if should be created it's parent directories if doesn't exists.
   * @return The relative path to the external public files directory.
   */
  public File getExternalRelativeDir (String path)
  {
    return getRelativeDir(path, true, true);
  }
  
  /**
   * Gets the MIME type from the given file path.
   * @param url     The file path as string.
   * @return The MIME type.
   */
  public String getMimeType (String url)
  {
    final String filepath = url.toLowerCase(Locale.getDefault());
    final String extension = MimeTypeMap.getFileExtensionFromUrl(filepath);
    String type = "text/plain";
    if (extension != null) {
      type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
  }
  
  /**
   * Gets the MIME type from the given File instance.
   * @param file    The File instance.
   * @return The MIME type.
   */
  public String getMimeType (File file)
  {
    return getMimeType( Uri.fromFile(file).toString() );
  }
  
  /**
   * Gets the MIME type from the given file URI.
   * @param file    The file URI.
   * @return The MIME type.
   */
  public String getMimeType (Uri file)
  {
    return getMimeType( file.toString() );
  }
  
  /**
   * Gets the android Intent pointing to the specified file with the specified MIME type.
   * @param fileuri    The file path as URI.
   * @param mime       The MIME type.
   * @return The Android Intent.
   * @see Intent
   */
  public Intent openFileIntent (Uri fileuri, String mime)
  {
    final Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType( fileuri, mime );
    return intent;
  }
  
  /**
   * Gets the android Intent pointing to the specified file without MIME type.
   * @param fileuri    The file path as URI.
   * @return The Android Intent.
   * @see #openFileIntent(Uri, String)
   */
  public Intent openFileIntent (Uri fileuri)
  {
    return openFileIntent(fileuri, getMimeType(fileuri) );
  }
  
  /**
   * Gets the android Intent pointing to the specified file with the specified MIME type.
   * @param file    The File instance.
   * @param mime    The MIME type.
   * @return The Android Intent.
   * @see #openFileIntent(Uri, String)
   */
  public Intent openFileIntent (File file, String mime)
  {
    return openFileIntent( Uri.fromFile(file), mime );
  }
  
  /**
   * Gets the android Intent pointing to the specified file without MIME type.
   * @param file    The File instance.
   * @return The Android Intent.
   * @see #openFileIntent(Uri, String)
   */
  public Intent openFileIntent (File file)
  {
    return openFileIntent( Uri.fromFile(file), getMimeType(file) );
  }
  
  /**
   * Gets the android Intent pointing to the specified file with the specified MIME type.
   * @param file    The file path as String.
   * @param mime    The MIME type.
   * @return The Android Intent.
   * @see #openFileIntent(Uri, String)
   */
  public Intent openFileIntent (String filepath, String mime)
  {
    return openFileIntent( Uri.parse(filepath), mime );
  }
  
  /**
   * Gets the android Intent pointing to the specified file without MIME type.
   * @param file    The file path as String.
   * @return The Android Intent.
   * @see #openFileIntent(Uri, String)
   */
  public Intent openFileIntent (String filepath)
  {
    return openFileIntent( Uri.parse(filepath), getMimeType(filepath) );
  }
  
}




Java Source Code List

git.egatuts.android.FileUtils.EgaCursorLoader.java
git.egatuts.android.FileUtils.EgaFileSystemBase.java
git.egatuts.android.FileUtils.EgaFileSystem.java
git.egatuts.android.FileUtils.EgaStorageUtils.java
git.egatuts.android.fragment.EgaFragmentActivityBase.java
git.egatuts.android.fragment.EgaFragmentActivity.java
git.egatuts.android.utils.EgaActivityManager.java
git.egatuts.exifeditor.MainActivity.java