存储在Android .txt文件 [英] Storing .txt files on Android

查看:264
本文介绍了存储在Android .txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我做了存储了一堆在Android上的资产文件夹.txt文件的严重的错误却发现它是只读的,我需要能够写入其中的一些,别人删除和添加新的在需要的时候的。

So I made the grave error of storing a bunch of .txt files in the Assets folder on Android only to discover that it is read-only and I need to be able to write to some of them, delete others and add new ones when needed.

我知道我可以在内部或外部存储它在SD卡上。
如果我存储在内部,在这里我把我所有的文件吗?

I know I can store it internally or externally on the SD card. If I store it internally, where do I place all of my files?

在外部更好的主意吗?

感谢

修改

它不是一个重要的问题,如果它们对用户可见

It isnt a major problem if they are visible to the user

推荐答案

下面是一个简单的code I用于获取存储目录:

Here is a sample code i use for getting the storage directory:

/**
 * Check if external storage is built-in or removable.
 *
 * @return True if external storage is removable (like an SD card), false
 *         otherwise.
 */
@TargetApi(9)
public static boolean isExternalStorageRemovable() {
    if (hasGingerbread()) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}

/**
 * Get the external app cache directory.
 *
 * @param context The context to use
 * @return The external cache dir
 */
@TargetApi(8)
public static File getExternalCacheDir(Context context) {
    if (hasFroyo()) {
        return context.getExternalCacheDir();
    }

    // Before Froyo we need to construct the external cache dir ourselves
    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
    return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}



/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use
 * @param uniqueName A unique directory name to append to the cache dir
 * @return The cache dir
 */
public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath =
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                    !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
                            context.getCacheDir().getPath();

    return new File(cachePath + File.separator + uniqueName);
}

和你有你自己把你的文件的路径....

And you got yourself a path to place your files....

现在,将它们放置在安装时,应在第一次运行从你的资产将它们复制到该文件夹​​,或者创建他们并填充他们根据自己的需要....没有没有其他办法有运文件与您的应用程序,他们可以来的资产,你可以从你的应用程序创建它们,或者从某些服务器下载它们。

And now to place them upon install, you should copy them on first run from your assets to that folder, or create them and populate them depending on your needs....and no there is no other way to have files shipped with your app, they can come in assets, you can create them from your app, or download them from some server.

EDIT1:该文件夹将成为Android的/数据/ your_package_name /你希望缓存+什么...

the folder will be in Android/data/your_package_name/cache+anything you want...

和用于姜饼和Froyo的两个功能:

and the two functions used for gingerbread and froyo:

 public static boolean hasFroyo() {
    // Can use static final constants like FROYO, declared in later versions
    // of the OS since they are inlined at compile time. This is guaranteed behavior.
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
}

public static boolean hasGingerbread() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
}

这篇关于存储在Android .txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆