This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

My Android app needs to read a value from an XML file stored in shared_prefs under another app.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,
My Android app needs to read a value from an XML file stored in shared_prefs under another app and store it in a parameter in my apps code. So I have the path to the file but I don't know how to code it:

the XML file is like:

 
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to Android's developer's documentation on using Shared Preferences:
https://developer.android.com/training/data-storage/shared-preferences#java

See the topics:
- Get handle to shared preferences
- Read from shared preferences

The topics (and example Java code) are about getting access to an existing shared preferences file and reading from it, respectively.
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pol Denais wrote:HI All,
My Android app needs to read a value from an XML file stored in shared_prefs under another app and store it in a parameter in my apps code. So I have the path to the file but I don't know how to code it: the XML file is like:



The example below creates a new shared preference file in an app, adds data to it and reads it from a second app.

Create data in the Shared Preferences in the first app:



When a shared preference file is created and is stored in the app data folder as an XML file with the format as shown below. The above Java code creates a shared preferences file MyNamePrefs and adds two entries to it:




Read from the Shared Preferences in the second app:



The log will print "My name is Pr Saya".

In your case, you just have to access the shared preference file and read the data in it.
 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prasad,

When application 2 is reading from Application 1's shared pref file (XML), how does it know what location to look ? Application 1s shared pref files sits under its folder on the device.


 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When application 2 is reading from Application 1's shared pref file (XML), how does it know what location to look ?



It is a storage feature of the Android software - SharedPrefrences. One do not have to specify the location (like a file path) of a shared preference. Just refer the name of the shared preference. One can create multiple shared preferences.

Shared preferences are mainly used to store small amounts of data in the XML format as key-value pairs. Only primitive data types can be stored - int, long, float, boolean and string. For example, if you have a set of applications like messaging,  contacts and phone - they may share info like a color (which can be stored as string hex value). Obviously important or security sensitive data cannot be stored. Also, it is not the place where app's data is stored.

 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prasad, Im just trying to debug it as my code is showing no value returned and when I debug, I can see a path location for my own application, in that its looking for application1_conf.xml under my own app's folder:

/data/user/0/com.nasher/shared_prefs/application1_conf.xml

I thought it should be looking for:

/data/user/0/com.application1/shared_prefs/application1_conf.xml
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My post: The example below creates a new shared preference file in an app, adds data to it and reads it from a second app...



The example code I had posted is what I have tested using Android Studio 3.1 and on a latest Android mobile device. I did not refer to any file paths in that. Verify the code again. Also, I did not look for (or debug) the path and where its stored on the device. It is not required - that is all about the Shared Preferences. Also, note SharedPreferences is different from Preferences (which is used for settings). I don't think the file path has any relevance.

I cannot say much about why your code is not able to get the stored shared preference. Can you post your app's code here:
- that created the shared preference, writing the data and the reading the saved data.
 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so the code in Application 1 uses the following:





In my application I was using the following code:




Thanks in advance
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pol Denais wrote:... In my application I was using the following code:
...



Now, print the value in the variable key in the statement (what is it printing?): String key = prefs.getString("ITEM1", "Couldn't Find");
 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I put in Log.i("mainActivity", "My key is " + key);


and when I debug I can see, key is "Couldn't find"
 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what the problem is. But, you can try this once more (if possible):

You will need two apps to test this. In the first app, in the main activity use the following code:



In the second app, in the main activity use the following code:



And, post the results here. NOTE: please use the code as it is; do not change it.
 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Still no joy. It finds the package name now but still returns null

 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is the mFile value


/data/user/0/com.application1/shared_prefs/app1_conf.xml



Should it not be :

/data/data/com.application1/shared_prefs/app1_conf.xml
 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thought I could try this instead below in an easier way but pin is showing null :


 
Prasad Saya
Rancher
Posts: 507
15
Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why is the mFile value /data/user/0/com.application1/shared_prefs/app1_conf.xml
Should it not be : /data/data/com.application1/shared_prefs/app1_conf.xml



I don't know much about how Android stores its files internally. Also, it may vary with different versions of the OS. Its possible one may not be able access these files directly without proper permissions (may require root permission, etc.. to get access directly).

so far, I can't figure what you are doing in your app regarding storing the shared preferences and retrieving them. Here are some links which you may find useful and help to figure whats happenning with your app. Hope this helps.

According to this post: /data/user/0 is a symbolic link of /data/data
A post to find the path: http://android.okhelp.cz/shared-preferences-file-path-android/
Android documentation on exploring the files on the device: https://developer.android.com/studio/debug/device-file-explorer
Where are shared preferences stored: https://stackoverflow.com/questions/6146106/where-are-shared-preferences-stored
How to View an Android Shared Preferences File: https://dzone.com/articles/how-view-android-shared
 
Pol Denais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prasad, Yeah I think I'm looking at the wrong idea. Could be I need to look at content provider to read this value.
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!