如何使一个APK创建的SD卡文件安装APK什么时候? [英] How do you make an apk create a file on the sdcard when the apk is installed?
问题描述
我很新的android开发并正在寻找一种方式来修改现有的Eclipse中源 - code,这样在安装APK时,XML文件是从里面APK复制到特定的文件夹上外置储存。
I am very new to android development and am looking for a way to modify existing source-code in eclipse so that when the apk is installed, an xml file is copied from inside the apk to a specific folder on the external storage.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
查看问题和答案在这里... <一个href=\"http://stackoverflow.com/questions/3851712/android-how-to-create-a-directory-on-the-sd-card-and-copy-files-from-res-raw-to\">Android:如何创建对SD卡的目录和/ RES文件拷贝/生吧?
See the question and answer here...Android: How to create a directory on the SD Card and copy files from /res/raw to it??
修改:关于它的思考,我用/资产的文件夹,而不是/ RES /生。这大约是我做什么...
EDIT: Thinking about it, I use the /assets folder and not /res/raw. This is roughly what I do...
首先得到你的外部存储(通常是SD卡)...
First get a valid folder on your external storage (normally SD card)...
File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.mycompany.myApp/files");
替换 com.mycompany.myApp
在上面的应用程序的包名称的路径。
Replace com.mycompany.myApp
in the path above with your app's package name.
那么下面将所有文件从资源文件夹复制与文件名以XYZ开头,例如,xyz123.txt,xyz456.xml等。
Then the following will copy all files from the assets folder with filenames beginning with "xyz", e.g., xyz123.txt, xyz456.xml etc.
try {
AssetManager am = getAssets();
String[] list = am.list("");
for (String s:list) {
if (s.startsWith("xyz")) {
Log.d(TAG, "Copying asset file " + s);
InputStream inStream = am.open(s);
int size = inStream.available();
byte[] buffer = new byte[size];
inStream.read(buffer);
inStream.close();
FileOutputStream fos = new FileOutputStream(myFilesDir + "/" + s);
fos.write(buffer);
fos.close();
}
}
}
catch (Exception e) {
// Better to handle specific exceptions such as IOException etc
// as this is just a catch-all
}
请注意,您需要以下权限在AndroidManifest.xml文件...
Note you'll need the following permission in the AndroidManifest.xml file...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于如何使一个APK创建的SD卡文件安装APK什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!