Android: Linking external file to Eclipse project - android

The application that I work on has a external SQLite database (sitting in the assets folder). My friend is using the same DB file in the iPhone version of the app. Content of the DB file is updated continuously. As both projects are in the same repository we created a Shared folder where we keep the DB file so both of use can link to that shared resource. It works in the iPhone project but fails in Android.
When in Eclipse, I click on the assets/new/file and click on Advanced and then Link to file in the file system. The file appears in the assets folder (in Eclipse) but I cannot access it from JAVA code.
Why that doesn't work? Is there any other was of linking external files to the project in Eclipse?
Thanks
EDITED:
I use this code for opening the assets file:
OutputStream os = new FileOutputStream(db_path + DB_NAME);
byte []b = new byte[1024];
int i, r;
//load list of files from 'data' folder
String[] fileCollection = am.list("data");
Arrays.sort(fileCollection);
for(i=0;i<fileCollection.length;i++)
{
//String fn = String.format(DB_NAME"%dd.db", (i + 1));
String fn = DB_NAME + "." + (i + 1);
if(fileCollection[i].equals(fn) == false){
break;
}
InputStream is = am.open("data/"+fn);
while((r = is.read(b)) != -1) {
os.write(b, 0, r);
}
is.close();
}
os.close();

From, the detail you given in question, I conclude that you can not use database file directly from the asset directory, you have to copy that database file into application's internal storage data/data/database/ and then use it.
EDIT:
I think Android environment can't recognize the physical path of you system files, so when we try to link any file for asset or any folder which are in android project hierarchy then it can not find the file which one linked from a system path.
So to make it working in android you have to put that file in your asset directory physically, not virtually (by putting file link in asset).
Hope I am not wrong in this. If yes then let me know on this topic.
Thanks.

Related

Assets folder not upgraded when installing new app

I have an app which has a .db (sql database) inside the "assets" folder.
When I need to update this database, I simply copy the new version in the "assets" folder, change the user version of the .db file and rebuild the apk. At runtime the application deletes the file in memory and copy exactly the one in the "assets" folder, then it checks if the version of this file is greater than the last one it had in memory (in another database) and, in that case, it does some stuff (which I'm skipping, since is not the point of the question).
The problem is: my application seems to ignore this new file, maintaining the old file in the "assets" folder, so naturally no change is detected.
It seems like my new file is not present in the apk, but only in devices with Android 9.
Is something changed with the usage of the assets folder that I am missing?
How can I arbitrarily update a file inside my apk when packing and upgrade?
This exact process works fine for older devices, then I had to upgrade the target sdk version to 28 and I got this problem while testing on an Android 9 (Pie) device.
Here I delete older file:
File l_DatabaseDir = p_Context.getDir("Database", Context.MODE_PRIVATE);
File l_DatabaseFile = new File(l_DatabaseDir, "MainDatabase.db");
File l_PlayersFile = new File(l_DatabaseDir, "players.db");
l_PlayersFile.delete();
Here I copy the file from the assets folder:
try
{
InputStream inputStream = p_Context.getAssets().open("players.db");
//Create the file before
OutputStream outputStream = new FileOutputStream(l_PlayersFile);
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
Finally I try to get the current version of the db:
m_PlayersDatabase = SQLiteDatabase.openDatabase(l_PlayersFile.toString(), null, SQLiteDatabase.OPEN_READWRITE);
int playersVersion = m_PlayersDatabase.getVersion();
Result on android 9: db version is the same as the first apk installed
Result on android 8: db version is the one of the file included in the new apk
EDIT: after further investigating it seems that the problem is more related to android memory, because simply changing the database file name (basically the destination of the copy)
l_PlayersFile = new File(l_DatabaseDir, "newplayers.db");
makes the application read the correct file also in android 9, so it looks like maintaining the same file name as the old one is causing the problem (Even if both the deletion and the copy of the new file are completed successfully).
So the assets folder update is fine.
The issue is only postponed to the next update though, since using again the name "newplayers.db" causes the exact same problem.
Generating a random name at every opening is quite brutal as workaround, any ideas?
Solved as I stated in the edit: copy the file from the assets folder with a different name (in my case with a timestamp).
You can change the assets folder's name eg:dist->dist1,then assets will be upgraded.

How do I Install external APKs necessary for my application in Android Studio?

I have downloaded CSipSimple . Now for video call of this , I need to install CSipSimple-Codec-Pack and CSipSimple-Video-plugin apks . I need to install these two external apks with my Android application . These apks are necessary for installation of my application .
How can I install these apks with my Android application by programming ?
there are 2 ways you could try this
Since the code of your dependent apps are open source, get the apks put them in your application's assets directory, then when your app is run copy the apks of the other apps to the external storage and initiate install
if both those apps are using gradle build
then you can git clone the source of the apps, build them, then import them as modules to your application, that way when your app is installed these other apps are also installed along with yours
The 1st one only works when phones have sdcards, the 2nd one is simpler its more straight forward
There is no code for this
I assume you know how to get the source code of the applications you need right?
just copy paste them into your application's root folder, then in android studio right click your project mouse over new-> click on module -> import gradle project -> then select the required application.
For the 1st method take a look at this thread
Code Snippet:
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("myapk.apk");
out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/myapk.apk");
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/myapk.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
} catch(Exception e) { }
The above code copies apk from your assests folder to sdcard, then installs it on the phone.

Android files in jar library not accessible when using with Unity

I am new to Unity. I have an Android library jar that I want to offer also as a plugin with Unity.
Within the library jar I have a folder html which contains a file temp.json. In the jar file (if I unzip it) the structure is like this: html/temp.json (Note here that jar works fine when running with any Android app outside Unity)
These are the steps that I followed to call my library through Unity:
1) Created folder Assets/Plugins/ Android in my Unity project hierarchy
2) Placed my library jar file there along with AndroidManifest.xml
3) Created a bridge.cs file that I use to call the functions in the jar file
Functions from the jar are called, but within the code of the jar somewhere on the Android side during the library lifecycle I call:
InputStream inputStream = MyClass.class.getClassLoader()
.getResourceAsStream("html/temp.json");
and I get a ResourceNotFound exception
From what I have read Unity ignores these files(like .json) when packaging. Therefore from what I understood in order to keep them in the Jar when packaging I created the following Unity structure:
Assets/Plugins/Android/assets/StreamingAssets/html/temp.json
I realise that any file that is placed within the StreamingAssets folder get copied in the assets folder of the library.
and in Java I call now:
inputStream = activity.getResources().getAssets().open("html/temp.json");
However I still get an exception that the file is not found.
Can anyone please help me/explain to me the procedure I have to follow to be able to read these files on the Java side while executing on Unity for Android?
Try this code:
private void readJson() {
String json = null;
try {
InputStream is = getAssets().open("html/temp.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
}
}
And save your json file to:
Assets/Plugins/Android/assets/html/temp.json
If you still can not do it, plz contact me
Br,
Frank

Trouble Finding CSV File (with and without OpenCSV) in Eclipse for Android

I'm having trouble reading from a csv file for my Android project. I'm using a Mac, with Eclipse ADT, and have imported OpenCSV.
The problem that I keep running into is that the file is not found. I have tried putting it everywhere, including: in the root folder (where the src folder is located), inside the src folder, and inside res/raw. I have refreshed the view, I have cleaned and rebuilt the project, I have restarted Eclipse, I have tried importing the file by drag and drop as well as by using the import option. For some reason, it still refuses to be found.
When I look at its path and absolute path (using file.getPath() and file.getAbsolutePath() they are: "abc.csv" and "/abc.csv" respectively. I have also double checked the file name, it is not named abc.csv.csv or anything similar.
I've used the following lines of code and these are the ones that have returned the error:
(This is from the OpenCSV site.)
CSVReader reader = new CSVReader(new FileReader("abc.csv"));
(#2 and #3 are not from OpenCSV and are just me experimenting around.)
AssetFileDescriptor descriptor = getAssets().openFd("abc.csv");
CSVReader reader = new CSVReader(new FileReader(descriptor.getFileDescriptor()));
3.
File file = new File("abc.csv");
try
{
Scanner inputStream = new Scanner(file);
while(inputStream.hasNext())
{
String data = inputStream.next();
}
inputStream.close();
}
catch(FileNotFoundException e)
{
Log.i(TAG, "File not found.");
}
Place the file in the assets folder and use AssetManager.

Can't locate file context.getDir("data", ...) in Android

I have a db located at
ctx.getApplicationContext().getDir("data", 0) + "/" + "db.db4o";
How can I browse this file with an file manager or via USB? I can't seem to find it :<
Browse with file manager, or USB, what does that mean? You can access the data directory, through a file manager, only if you are on a rooted device or an emulator.
Your application specific file are not directly stored in the data directory, but
data/data/your_package_name
you could create an "asset" folder into your android project and put it there. Before that you can try with this:
InputStream is = getAssets().open("db.db4o");
And convert "is" if is necessary.
Regards

Categories

Resources