2013-12-12 41 views
0

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

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

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

感谢

编辑

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

+0

取决于你需要他们对于。如果您将它们存储在SD卡的外部,它们将可以被用户操作。如果你在内部存储他们,他们将不会,他们将是你的私人。在这两种情况下,只要您的应用程序具有正确的权限,您就可以读取/写入它们。这里有你的选择:http://developer.android.com/guide/topics/data/data-storage.html –

+0

在我的Java项目中,我将在那里移动我所有的txt文件。 PRIVATE/INTERNAL文件夹在哪里? –

+0

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal您必须像这样以编程方式访问它。 –

回答

0

这里看到是一个示例代码,我用得到的存储目录:

/** 
* 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的/数据/ your_package_name /缓存+任何你想要的......

,并用于姜饼和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; 
}