Android的内部内存复制文件 [英] Copy files inside Android internal memory

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

问题描述

我写一个Android应用程序,我需要两个文件拷贝到设备的内部存储。目前,我可以手动运行后,我的应用程序的设备的/数据/数据​​/ [my_package_name] /文件下复制这两个文件夹使用DDMS。我需要把这两个文件到该文件夹​​(或任何文件夹),而被安装在设备上我的应用程序。该这里小费说内的RES / RAW文件/目录将被部署到该设备并且能够通过openRawResource(),但它不'吨告诉这些文件将在内存里面放。要summerize,我在我的项目文件夹(资产,原材料,其他任何文件夹)两个文件,​​我需要这两个拷贝的内部存储器的路径内。我怎样才能做到这一点?我是否需要纳入到我的Andr​​oidManifest.xml中的任何权限?

I am writing an Android application and I need to copy 2 files into the device' s internal storage. Currently I am able to run my application after manually copying these two files under the device' s "/data/data/[my_package_name]/files" folder using DDMS. I need to put these two files into that folder(or any folder) while my application is being installed on the device. The tip here says files within the "res/raw/" directory will be deployed to the device and will be accessible through openRawResource(), but it doesn' t tell where these files will be put inside the internal memory. To summerize, I have two files in my project folder(assets, raw, any other folder) and I need to copy these two inside a path of internal memory. How can I achieve this? Do I need any permissions included to my AndroidManifest.xml?

感谢您提前

推荐答案

没有什么大不了的。这样做有几种方法,也许比这短,虽然。

No big deal. There are several ways of doing this, probably shorter than this one, though.

InputStream is = AssetManager.open("somefile.txt");
File outputFile = new File(getExternalFilesDir(), "somefile.txt");
OutputStream os = new FileOutputStream(outputFile);

//Now copy is to os. I'd recommend using Apache Commons IO
org.apache.commons.io.IOUtils.copy(is, os));

是的,你需要的权限来写入SD卡(WRITE_EXTERNAL_STORAG​​E)。

And yes, you need permissions to write to SDcard (WRITE_EXTERNAL_STORAGE).

更新:结果
只是为了澄清:什么尖端说的是:结果

UPDATE:
Just to clarify: What the tip says is:

提示:如果您想保存在编译时在应用程序中的静态文件,将文件保存在您的项目RES /生/目录下。你可以用openRawResource()打开它,传递R.raw。资源ID。该方法返回一个InputStream,你可以用它来读取该文件(但不能写入原始文件)。

在资产文件夹中的文件都包含在APK,但这些是只读的,它们永远不会部署到内部或外部存储器。如果你需要做的,你必须自己做,如上面的例子。

The files in assets folder are included in the apk, but these are read only and they are never deployed to the internal or external memory. If you need to do that you'll have to do it by yourself, as shown in the example above.

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

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