【问题标题】:Storing .txt files on Android在 Android 上存储 .txt 文件
【发布时间】:2013-12-12 19:08:08
【问题描述】:

所以我犯了一个严重的错误,将一堆 .txt 文件存储在 Android 上的 Assets 文件夹中,结果发现它是只读的,我需要能够写入其中一些文件,删除其他文件并添加新文件需要的时候。

我知道我可以将其存储在内部或外部的 SD 卡上。 如果我将其存储在内部,我应该将所有文件放在哪里?

在这里外部是一个更好的主意吗?

谢谢

编辑

如果它们对用户可见,这不是一个大问题

【问题讨论】:

  • 取决于你需要它们做什么。如果您将它们外部存储在 SD 卡上,它们将是可见的/能够被用户操作。如果您将它们存储在内部,它们不会是,它们将是您私有的。在这两种情况下,只要您的应用程序具有正确的权限,您就可以读取/写入它们。以下是您的选择:developer.android.com/guide/topics/data/data-storage.html
  • 在我的 Java 项目中,我会将我的所有 txt 文件移动到哪里。 PRIVATE/INTERNAL 文件夹在哪里?
  • developer.android.com/guide/topics/data/… 你必须像这样以编程方式访问它。

标签: android


【解决方案1】:

这是我用于获取存储目录的示例代码:

/**
 * 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);
}

而且你为自己找到了放置文件的路径....

现在要在安装时放置它们,您应该在第一次运行时将它们从您的资产复制到该文件夹​​,或者根据您的需要创建它们并填充它们....没有其他方法可以发送文件对于您的应用,它们可以进入资产,您可以从您的应用创建它们,或从某个服务器下载它们。

Edit1:文件夹将位于 Android/data/your_package_name/cache+anything you want...

以及用于姜饼和 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2020-10-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多