Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;

import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class Main {
    public static final String THUMBNAILS_FOLDER = ".thumbnails";
    public static final String MEDIA_PUBLIC_FOLDER_NAME = "AR.Drone";
    public static final String NO_MEDIA_FILE = ".no_media";
    private static final String TAG = "FileUtils";

    public static File getMediaThumbFolder(Context context) {
        File mediaThumbFolder = new File(getMediaFolder(context), THUMBNAILS_FOLDER);

        if (mediaThumbFolder != null) {

            if (!mediaThumbFolder.exists()) {
                mediaThumbFolder.mkdirs();
                Log.d(TAG, "Thumbnails folder created " + mediaThumbFolder);
            }

            createNoMediaFile(mediaThumbFolder);
        }
        return mediaThumbFolder;
    }

    /**
     * Retrieves FreeFlight media directory.
     * May return null.
     * @param context
     * @return Media directory to store the media files or null if sd card is not mounted.
     */
    public static File getMediaFolder(Context context) {
        File dcimFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

        if (dcimFolder == null) {
            Log.w(TAG, "Looks like sd card is not available.");
            return null;
        }

        File mediaFolder = new File(dcimFolder, MEDIA_PUBLIC_FOLDER_NAME);

        if (!mediaFolder.exists()) {
            mediaFolder.mkdirs();
            Log.d(TAG, "Root media folder created " + mediaFolder);
        }

        return mediaFolder;
    }

    private static void createNoMediaFile(File file) {
        try {
            File noMediaFile = new File(file, NO_MEDIA_FILE);

            if (!noMediaFile.exists()) {
                noMediaFile.createNewFile();
            }
        } catch (IOException e) {
            Log.w(TAG, e.toString());
        }
    }
}