I'm developing an android application that store sql files to update the sqlite database. And that sql script will run when user push certain button. This is where i put my sql files
So far i've been using this solution to run my script: First, mapping my update to resource as update.xml
<resources>
<string name="update_list">
v2_9_11
</string>
<string-array name="v2_9_11">
<item>1_AlterTable_wapactivity</item>
</string-array>
</resources>
And get the sql script inside the file with this method :
private void runScript(String version_name){
try{
String[] sql_list = mGap.getResources().getStringArray(getArrayIdentifier(mGap, version_name));
for (String sql : sql_list) {
InputStream in = getClass().getResourceAsStream("/com/ods/scm/script/update/"+version_name+"/"+sql+".sql");
Reader fr = new InputStreamReader(in, "utf-8");
BufferedReader br = new BufferedReader(fr);
String s="";
String baris;
while((baris=br.readLine())!=null){
s+=baris;
}
mDbHelper.executeStatement(s);
}
insertUpdate(version_name);
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
I get the String version_name and String[ ] sql_list file name from update.xml. The problem is, i want to get version_name ( in this case v2_9_11 as subfolder name from folder update ) and sql file name ( in this case 1_AlterTable_wapactivity.sql ) without mapping it in update.xml. Can ayone help me?
EDIT
Or can anyone direct me how to automatically mapping it to update.xml when build the application?
I suggest you put the .sql file in assets
folder. By doing that, you are free to manipulate your file with File IO API.
Sorry all, i just now put it in bounty but i'm able to resolve it by myself by the short time after i put it in bounty. Anyway, my solution is to open the apk file that inside the android device (data/app/....apk) as a zip stream. Then list all the file that start with com/ods/scm/script/update
. Here my method to get all file that start with it :
public ArrayList openApk(String filePath){
ArrayList<String> list = new ArrayList<String>();
FileInputStream fis = null;
ZipInputStream zipIs = null;
ZipEntry zEntry = null;
try {
fis = new FileInputStream(filePath);
zipIs = new ZipInputStream(new BufferedInputStream(fis));
while((zEntry = zipIs.getNextEntry()) != null){
if(zEntry.getName().startsWith("com/ods/scm/script/update/"))
list.add(zEntry.getName());
}
zipIs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
来源:https://stackoverflow.com/questions/28206507/getting-list-of-subfolders-name-and-files-name-in-android-source-package