Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String AudioFolderName = "UDeskAudio";

    public static File getAudioFile(Context context, String url) {
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        String path = getOutputAudioPath(context, fileName);
        if (TextUtils.isEmpty(path)) {
            return null;
        } else {
            return new File(path);
        }
    }

    public static String getOutputAudioPath(Context context) {
        return getOutputAudioPath(context, "audio_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()));
    }

    public static String getOutputAudioPath(Context context, String mediaName) {
        File mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES),
                AudioFolderName);

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        File noMediaFile = new File(mediaStorageDir, ".nomedia");
        if (!noMediaFile.exists()) {
            try {
                noMediaFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return mediaStorageDir.getPath() + File.separator + mediaName;
    }
}