Android文件操作工具类

浏览:
字体:
发布时间:2013-12-21 10:33:06
来源:

MTK中看到的一个文件操作的工具类贴出来大家使用节省时间。

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import com.android.videoeditor.R;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Bitmap.CompressFormat;import android.media.videoeditor.MediaProperties;import android.os.Environment;import android.util.Log;/** * File utilities */public class FileUtils {    // Logging    private static final String TAG = "FileUtils";    /**     * It is not possible to instantiate this class     */    private FileUtils() {    }    /**     * Gets the root path for all projects     *     * @param context The context     *     * @return The file representing the projects root directory, {@code null} if the external     * storage is not currnetly mounted     */    public static File getProjectsRootDir(Context context)            throws FileNotFoundException, IOException {        final File dir = MtkUtils.getExternalFilesDir(context);//context.getExternalFilesDir(null);        if (dir != null && !dir.exists()) {            if (!dir.mkdirs()) {                throw new FileNotFoundException("Cannot create folder: " + dir.getAbsolutePath());            } else {                // Create the file which hides the media files                if (!new File(dir, ".nomedia").createNewFile()) {                    throw new FileNotFoundException("Cannot create file .nomedia");                }            }        }        return dir;    }    /**     * Get the filename for the specified raw resource id. Create the file if     * the file does not exist.     *     * @param context The context     * @param maskRawResourceId The mask raw resource id     *     * @return The mask filename     */    public static String getMaskFilename(Context context, int maskRawResourceId)            throws FileNotFoundException, IOException {        final String filename;        switch (maskRawResourceId) {            case R.raw.mask_contour: {                filename = "mask_countour.jpg";                break;            }            case R.raw.mask_diagonal: {                filename = "mask_diagonal.jpg";                break;            }            default: {                throw new IllegalArgumentException("Invalid mask raw resource id");            }        }        final File mf = new File(context.getFilesDir(), filename);        if (!mf.exists()) {            Bitmap bitmap = null;            FileOutputStream fos = null;            InputStream is = null;            try {                is = context.getResources().openRawResource(maskRawResourceId);                bitmap = BitmapFactory.decodeStream(is);                if (bitmap == null) {                    throw new IllegalStateException("Cannot decode raw resource mask");                }                fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);                if (!bitmap.compress(CompressFormat.JPEG, 100, fos)) {                    throw new IllegalStateException("Cannot compress bitmap");                }            } finally {                if (is != null) {                    is.close();                }                if (bitmap != null) {                    bitmap.recycle();                }                if (fos != null) {                    fos.flush();                    fos.close();                }            }        }        return mf.getAbsolutePath();    }    /**     * Get the raw id for the mask file     *     * @param path The full file name     *     * @return The raw id     */    public static int getMaskRawId(String path) {        final String filename = new File(path).getName();        if (filename.equals("mask_countour.jpg")) {            return R.raw.mask_contour;        } else if (filename.equals("mask_diagonal.jpg")) {            return R.raw.mask_diagonal;        } else {            throw new IllegalArgumentException("Unknown file: " + path);        }    }    /**     * Get the filename for the specified raw resource id. Create the file if     * the file does not exist     *     * @param context The context     * @param rawResourceId The raw resource id     *     * @return The audio track filename     */    public static String getAudioTrackFilename(Context context, int rawResourceId)            throws FileNotFoundException, IOException {        final String filename;        switch (rawResourceId) {            case R.raw.theme_travel_audio_track: {                filename = "theme_travel.m4a";                break;            }            case R.raw.theme_surfing_audio_track: {                filename = "theme_surfing.m4a";                break;            }            case R.raw.theme_film_audio_track: {                filename = "theme_film.m4a";                break;            }            case R.raw.theme_rockandroll_audio_track: {                filename = "theme_rockandroll.m4a";                break;            }            default: {                throw new IllegalArgumentException("Invalid audio track raw resource id");            }        }        final File mf = new File(context.getFilesDir(), filename);        if (!mf.exists()) {            FileOutputStream fos = null;            InputStream is = null;            try {                is = context.getResources().openRawResource(rawResourceId);                fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);                final byte[] buffer = new byte[1024];                int bytesRead;                while ((bytesRead = is.read(buffer)) > 0) {                    fos.write(buffer, 0, bytesRead);                }            } finally {                if (is != null) {                    is.close();                }                if (fos != null) {                    fos.flush();                    fos.close();                }            }        }        return mf.getAbsolutePath();    }    /**     * Create a new project directory     *     * @return The absolute path to the project     */    public static String createNewProjectPath(Context context)            throws FileNotFoundException, IOException {        final File file = new File(getProjectsRootDir(context), StringUtils.randomString(10));        if (Log.isLoggable(TAG, Log.DEBUG)) {            Log.d(TAG, "New project: " + file.getAbsolutePath());        }        return file.getAbsolutePath();    }    /**     * Get a unique video filename.     *     * @param fileType The file type     *     * @return The filename     */    public static String createMovieName(int fileType) {        final String filename;        switch (fileType) {            case MediaProperties.FILE_MP4: {                filename = "movie_" + StringUtils.randomStringOfNumbers(6) + ".mp4";                break;            }            case MediaProperties.FILE_3GP: {                filename = "movie_" + StringUtils.randomStringOfNumbers(6) + ".3gp";                break;            }            default: {                throw new IllegalArgumentException("Unsupported file type: " + fileType);            }        }        /// M: support switching sdcard for exporting movie @{        // final File moviesDirectory = Environment.getExternalStoragePublicDirectory(        //        Environment.DIRECTORY_MOVIES);        final File moviesDirectory = new File(MtkUtils.getMovieExportPath());        /// @}        // Make this directory if it does not exist        if (!moviesDirectory.exists()) {            moviesDirectory.mkdirs();        }        final File f = new File(moviesDirectory, filename);        return f.getAbsolutePath();    }    /**     * Delete all the files in the specified folder and the folder itself.     *     * @param dir The project path     */    public static boolean deleteDir(File dir) {        if (dir.isDirectory()) {            final String[] children = dir.list();            for (int i = 0; i < children.length; i++) {                final File f = new File(dir, children[i]);                if (!deleteDir(f)) {                    Log.e(TAG, "File cannot be deleted: " + f.getAbsolutePath());                    return false;                }            }        }        // The directory is now empty so delete it        return dir.delete();    }    /**     * Get the name of the file     *     * @param filename The full path filename     * @return The name of the file     */    public static String getSimpleName(String filename) {        final int index = filename.lastIndexOf('/');        if (index == -1) {            return filename;        } else {            return filename.substring(index + 1);        }    }}




>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();