Android Open Source - android-data-persistence File Utilities






From Project

Back to project page android-data-persistence.

License

The source code is released under:

MIT License

If you think the Android project android-data-persistence 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

package com.teamtreehouse.mememaker.utils;
/*w w w .  j  ava2 s . co  m*/
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.teamtreehouse.mememaker.MemeMakerApplicationSettings;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Evan Anger on 7/28/14.
 */
public class FileUtilities {

    public static void saveAssetImage(Context context, String assetName) {
        File fileDirectory = getFileDirectory(context);
        File fileToWrite = new File(fileDirectory, assetName);

        AssetManager assetManager = context.getAssets();
        try {
            InputStream in = assetManager.open(assetName);
            FileOutputStream out = new FileOutputStream(fileToWrite);
            copyFile(in, out);
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static File getFileDirectory(Context context) {
        MemeMakerApplicationSettings settings = new MemeMakerApplicationSettings(context);
        String storageType = settings.getStoragePreference();
        if(storageType.equals(StorageType.INTERNAL)) {
            return context.getFilesDir();
        } else {
            if(isExternalStorageAvailable()) {
                if(storageType.equals(StorageType.PRIVATE_EXTERNAL)) {
                    return context.getExternalFilesDir(null);
                } else {
                    return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                }
            } else {
                return context.getFilesDir();
            }
        }
    }

    public static boolean isExternalStorageAvailable() {
        String state = Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    private static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

    public static File [] listFiles(Context context) {
        File fileDirectory = getFileDirectory(context);
        File [] filteredFiles = fileDirectory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                if(file.getAbsolutePath().contains(".jpg")) {
                    return true;
                } else {
                    return false;
                }
            }
        });

        return filteredFiles;
    }

    public static Uri saveImageForSharing(Context context, Bitmap bitmap,  String assetName) {
        File fileToWrite = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), assetName);

        try {
            FileOutputStream outputStream = new FileOutputStream(fileToWrite);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return Uri.fromFile(fileToWrite);
        }
    }


    public static void saveImage(Context context, Bitmap bitmap, String name) {
        File fileDirectory = getFileDirectory(context);
        File fileToWrite = new File(fileDirectory, name);

        try {
            FileOutputStream outputStream = new FileOutputStream(fileToWrite);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}




Java Source Code List

com.teamtreehouse.mememaker.MemeMakerApplicationSettings.java
com.teamtreehouse.mememaker.MemeMakerApplication.java
com.teamtreehouse.mememaker.adapters.GridViewAdapter.java
com.teamtreehouse.mememaker.adapters.MemeItemListAdapter.java
com.teamtreehouse.mememaker.adapters.SectionsPagerAdapter.java
com.teamtreehouse.mememaker.database.MemeDatasource.java
com.teamtreehouse.mememaker.database.MemeSQLiteHelper.java
com.teamtreehouse.mememaker.models.ImageGridItem.java
com.teamtreehouse.mememaker.models.ImageItem.java
com.teamtreehouse.mememaker.models.MemeAnnotation.java
com.teamtreehouse.mememaker.models.Meme.java
com.teamtreehouse.mememaker.ui.activities.CreateMemeActivity.java
com.teamtreehouse.mememaker.ui.activities.MainActivity.java
com.teamtreehouse.mememaker.ui.activities.MemeSettingsActivity.java
com.teamtreehouse.mememaker.ui.fragments.ImageGridFragment.java
com.teamtreehouse.mememaker.ui.fragments.MemeItemFragment.java
com.teamtreehouse.mememaker.ui.fragments.MemeSettingsFragment.java
com.teamtreehouse.mememaker.ui.views.MemeImageView.java
com.teamtreehouse.mememaker.utils.FileUtilities.java
com.teamtreehouse.mememaker.utils.ImageResizer.java
com.teamtreehouse.mememaker.utils.StorageType.java