Android Open Source - kirin-for-android S D Card File Utils






From Project

Back to project page kirin-for-android.

License

The source code is released under:

Apache License

If you think the Android project kirin-for-android 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

/*
   Copyright 2011 Future Platforms/* www  . ja  va2s .  c  o m*/

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/


package com.futureplatforms.kirin.internal.attic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;

import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.futureplatforms.kirin.C;

public class SDCardFileUtils {

    public File mBaseDir;

    private boolean mBaseDirAvailable = false;

    public SDCardFileUtils(String path) {
      mBaseDir = new File(path);
    }

    public void checkStorageAvailable() throws IOException {
        if (mBaseDirAvailable) {
            return;
        }

        synchronized (mBaseDir) {
            if (!mBaseDir.exists()) {
                mBaseDir.mkdirs();
            } else if (!mBaseDir.isDirectory()) {
                mBaseDir.delete();
                mBaseDir.mkdirs();
            }
    
            if (!mBaseDir.exists() || !mBaseDir.isDirectory() || !mBaseDir.canWrite() || !mBaseDir.canRead()) {
                throw new IOException("Unable to open directory in " + mBaseDir.getAbsolutePath() + " to store files.");
            }
            // we should make the basedir available right before we try and make a file. 
            // (otherwise we have stackoverflow).
            
            mBaseDirAvailable = true;
        }
        try {
            createFile(null, ".nomedia");
        } catch (IOException e) {
            // hum.
        }
    }

    public Uri uriFromFilename(Context context, String filename) throws IOException {
        // passing to an outside intent (crop picture, before wallpaper)
        File file = readableFile(context, filename);
        return Uri.fromFile(file);
    }

    public File readableFile(Context context, String filename) throws IOException {
        checkStorageAvailable();
        return new File(mBaseDir, filename);

        // File file = context.getFileStreamPath(filename);
        // return file;
        // return file;
        // return file;
    }

    private FileOutputStream fileOutputStream(Context context, String filename) throws IOException {
        // return context.openFileOutput(filename,
        // Context.MODE_WORLD_READABLE);

        File file = readableFile(context, filename);
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                boolean success = file.getParentFile().mkdirs();
                if (!success) {
                    throw new IOException("Cannot create a new file " + file.getParent());
                }
            }
            boolean success = file.createNewFile();
            if (!success) {
                throw new IOException("Cannot create a new file " + file.getPath());
            }
        }
        return new FileOutputStream(file);
    }

    public InputStream inputStream(Context context, String filename) throws IOException {
        File file = readableFile(context, filename);
        FileInputStream in = new FileInputStream(file);
        return in;
    }

    public int deleteFiles(Context context, Set<String> filenames) {
        int count = 0;
        for (String filename : filenames) {
            count += deleteFile(context, filename);
        }
        return count;
    }

    public int deleteFile(Context context, String filename) {
        File file = new File(mBaseDir, filename);
        if (file.exists() && file.canWrite()) {
            return file.delete() ? 1 : 0;
        }
        return 0;
    }

    private int recursivelyDelete(File file, Set<String> exceptions) {
        if (file == null) {
            return 0;
        }
        if (file.isFile()) {
            if (exceptions.contains(file.getName())) {
                return 0;
            } else {
                Log.d(C.TAG, "Deleting " + file.getName());
                return file.delete() ? 1 : 0;
            }
        } else if (file.isDirectory()) {
            int count = 0;
            for (File child : file.listFiles()) {
                count += recursivelyDelete(child, exceptions);
            }
            return count;

        } else {
            return 0;
        }
    }

    public int deleteEverything(Context context, Set<String> exceptions) {
        return recursivelyDelete(mBaseDir, exceptions);
    }

    public boolean fileExists(Context context, String filename) {
        try {
            return readableFile(context, filename).exists();
        } catch (IOException e) {
            return false;
        }
    }

    public OutputStream outputStream(Context context, String filename) throws IOException {
        // must be readable for cropping intent, though first used when
        // downloading an image.
        return fileOutputStream(context, filename);
    }

    public Uri createFile(Context context, String filename) throws IOException {
        File wallpaperFile = readableFile(context, filename);
        OutputStream out = null;
        try {
            out = fileOutputStream(context, filename);
            out.write(1);
        } finally {
            IOUtils.close(out);
        }

        return Uri.parse("file:/" + wallpaperFile.getAbsolutePath());

    }
}




Java Source Code List

com.futureplatforms.kirin.C.java
com.futureplatforms.kirin.Kirin.java
com.futureplatforms.kirin.TestingApplication.java
com.futureplatforms.kirin.activities.KirinActivity.java
com.futureplatforms.kirin.activities.KirinListActivity.java
com.futureplatforms.kirin.application.IKirinApplication.java
com.futureplatforms.kirin.application.KirinApplication.java
com.futureplatforms.kirin.extensions.IKirinExtensionOnNonDefaultThread.java
com.futureplatforms.kirin.extensions.IKirinExtensionOnUiThread.java
com.futureplatforms.kirin.extensions.IKirinExtension.java
com.futureplatforms.kirin.extensions.IProguardImmunity.java
com.futureplatforms.kirin.extensions.KirinExtensionAdapter.java
com.futureplatforms.kirin.extensions.KirinExtensions.java
com.futureplatforms.kirin.extensions.databases.DBStatement.java
com.futureplatforms.kirin.extensions.databases.DBTransaction.java
com.futureplatforms.kirin.extensions.databases.DatabasesBackend.java
com.futureplatforms.kirin.extensions.databases.IDatabasesBackend.java
com.futureplatforms.kirin.extensions.fs.KirinFilesystemExtensionImpl.java
com.futureplatforms.kirin.extensions.localnotifications.ILocalNotificationsBackend.java
com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationAlarmReceiver.java
com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationsBackend.java
com.futureplatforms.kirin.extensions.networking.INetworkingBackend.java
com.futureplatforms.kirin.extensions.networking.KirinXHRExtensionImpl.java
com.futureplatforms.kirin.extensions.networking.NetworkingBackend.java
com.futureplatforms.kirin.extensions.settings.ISettingsBackend.java
com.futureplatforms.kirin.extensions.settings.PreferencesBackendImpl.java
com.futureplatforms.kirin.extensions.settings.SettingsBackend.java
com.futureplatforms.kirin.generated.fs.KirinCallback.java
com.futureplatforms.kirin.generated.fs.KirinFile.java
com.futureplatforms.kirin.generated.fs.KirinFilesystemExtension.java
com.futureplatforms.kirin.generated.fs.KirinFilesystem.java
com.futureplatforms.kirin.generated.fs.KirinFoundFile.java
com.futureplatforms.kirin.generated.fs.KirinOptionalCallback.java
com.futureplatforms.kirin.generated.location.KirinLocationBackend.java
com.futureplatforms.kirin.generated.location.KirinLocationData.java
com.futureplatforms.kirin.generated.location.KirinLocationListener.java
com.futureplatforms.kirin.generated.location.KirinLocationPermissions.java
com.futureplatforms.kirin.generated.location.KirinLocation.java
com.futureplatforms.kirin.generated.preferences.KirinPreferenceListener.java
com.futureplatforms.kirin.generated.preferences.KirinPreferencesBackend.java
com.futureplatforms.kirin.generated.preferences.KirinPreferences.java
com.futureplatforms.kirin.generated.xhr.KirinXHRExtension.java
com.futureplatforms.kirin.generated.xhr.KirinXHRProgressEvent.java
com.futureplatforms.kirin.generated.xhr.KirinXHRResponse.java
com.futureplatforms.kirin.generated.xhr.KirinXHRequest.java
com.futureplatforms.kirin.generated.xhr.KirinXMLHTTPRequest.java
com.futureplatforms.kirin.helpers.IKirinExtensionHelper.java
com.futureplatforms.kirin.helpers.IKirinHelper.java
com.futureplatforms.kirin.helpers.KirinApplicationHelper.java
com.futureplatforms.kirin.helpers.KirinExtensionHelper.java
com.futureplatforms.kirin.helpers.KirinHelper.java
com.futureplatforms.kirin.helpers.KirinScreenHelper.java
com.futureplatforms.kirin.helpers.KirinUiFragmentHelper.java
com.futureplatforms.kirin.internal.JSONUtils.java
com.futureplatforms.kirin.internal.KirinPaths.java
com.futureplatforms.kirin.internal.attic.IOUtils.java
com.futureplatforms.kirin.internal.attic.KirinDropbox.java
com.futureplatforms.kirin.internal.attic.ProxyGenerator.java
com.futureplatforms.kirin.internal.attic.SDCardFileUtils.java
com.futureplatforms.kirin.internal.core.AbstractObjectHolder.java
com.futureplatforms.kirin.internal.core.DefaultObjectHandler.java
com.futureplatforms.kirin.internal.core.IJsContext.java
com.futureplatforms.kirin.internal.core.IKirinState.java
com.futureplatforms.kirin.internal.core.INativeContext.java
com.futureplatforms.kirin.internal.core.IObjectHolder.java
com.futureplatforms.kirin.internal.core.JsCommands.java
com.futureplatforms.kirin.internal.core.KirinAppState.java
com.futureplatforms.kirin.internal.core.KirinWebViewHolder.java
com.futureplatforms.kirin.internal.core.NativeContext.java
com.futureplatforms.kirin.internal.core.UiObjectHolder.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer4.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer5.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer.java
com.futureplatforms.kirin.internal.fragmentation.WebChromeClient7.java
com.futureplatforms.kirin.internal.fragmentation.WebChromeClient8.java
com.futureplatforms.kirin.state.IKirinDropbox.java
com.futureplatforms.kirin.state.IKirinFileSystem.java
com.futureplatforms.kirin.ui.DebugMenuActions.java
com.futureplatforms.kirin.ui.JSListAdapter.java
com.futureplatforms.kirin.ui.JSOnClickListener.java
com.futureplatforms.kirin.ui.KirinRowRenderer.java