How to get File within' package, in an Android application - java

I'm am creating an Android application, but in order to have one of the functionalities working I need to read a predefined xml file whilst only knowing its name, not the R.id..
In normal Java I know I can use
getClass().getClassLoader().getResource(xmlName)
but using the limited Android SDK thats not working, any knows how to solve this?

Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.

From the Data Storage Section in the android developer manual:
If you have a static file to package with your application at compile time, you can save
the file in your project in res/raw/myDataFile, and then open it with
Resources.openRawResource (R.raw.myDataFile). It returns an InputStream object that you can
use to read from the file.

Related

how to read a file without getAssets in java(android)

Does anybody knows how can i read a file without using getAssets();? I've pasted the file in my assets folder because I thought it would be possible to use this method. The problem is that I have to get the file inside a non-activity class and it's not practical to pass on the context as a parameter since I call this class +8x in my code.
I usually code in C# and I'm frustrated because every solution i see uses the GetAssets().
Thanks
Does anybody knows how can i read a file without using getAssets();?
You are not reading a file. You are reading an asset, based on:
I've pasted the file in my assets folder
Assets are files on your development machine. They are not files on the Android device. They are part of your APK, just like resources. And, yes, you need to use getAssets() to be able to access assets.
The problem is that I have to get the file inside a non-activity class
Set up dependency inversion (Dagger, Koin, etc.) and inject a Context into this "non-activity class". In other words, if you do not have access to a Context on which to call getAssets(), that is an architecture problem, not a programming problem.

java.lang.IllegalStateException: Not one of standard directories [duplicate]

Android Api 29 has big changes regarding files and folders.
I have not found a way so far to create a folder in the internal storage that is public visible.
Every folder I create can be seen by my app only.
I write an app that creates data files that shall be picked up by other apps, for instance the Total Commander to copy them to a destination.
Everything worked fine until I activated Api 29. Some of my clients DO use pixel phones and they use Android 10.
How can I create a folder and files in Android 10 that are public?
This has been deprecated:
Environment.getExternalStorageDirectory();
Environment.getExternalStoragePublicDirectory(type);
and when I use
File root = context.getExternalFilesDir(null);
The created files can only be seen by my app.
How can I achieve the behavior that was valid before Android 10?
Thanks in advance.
when I use File root = context.getExternalFilesDir(null); The created files can only be seen by my app
They can be seen by any app that uses the Storage Access Framework (e.g., ACTION_OPEN_DOCUMENT), if the user chooses the document that you place in that directory.
I write an app that creates data files that shall be picked up by other apps
Other apps have no access to external or removable storage on Android 10, except in the limited directories like getExternalFilesDir() or via non-filesystem means (ACTION_OPEN_DOCUMENT, MediaStore).
How can I create a folder and files in Android 10 that are public?
Use getExternalFilesDir() and related methods on Context. Or, use ACTION_CREATE_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE and use the Storage Access Framework. In either case, the resulting documents can be used by other apps that also use the Storage Access Framework.
Starting from Android 10, you should use SAF, and let user choose the directory using ACTION_OPEN_DOCUMENT_TREE.
If you need a simple example. You can find it here
Alternatively, you could use requestLegacyExternalStorage = true in manifest when your app is not newly released. But, this is something that should not be used for future release, as this is a short-term solution provided by Google.
Note: In future releases of Android, user will not be able to pick the whole external file directory and Downloads directory, so unfortunately, keep in mind that we are not going to have access to these as well! For more information you can click here

Android: Retrieving shared preferences of other application (rooted device)

I am running a vulnerable android application on a rooted device using the Genymotion and i am trying to read shared preferences and a file in files directory inside different android application like:
/data/data/xxxx/config.xml and
/data/data/xxx/files/xxxx.xml
i am trying to read this data programmatically using a sample java application to show the data in logcat, but when i try to read the files, i get permission issue on the logcat.
The funny thing is the application is running on a rooted device, so i suppose to have access to other applications sharedpreferences.I need something like this, but show this in the logcat:
https://lightsec.wordpress.com/2014/04/28/android-sharedpreferences-insecure-storage/.
I have also tries this answer, but it does not work:
Android: Retrieving shared preferences of other application
How can i retrieve all the available keys from sharedpreferences and show in logcat?
Both of those links describe using the WORLD_XX constants as in the app you're trying to read from.
Literally no one in the world would use those constants, as they don't want their shared preferences to be read by others. Everyone uses the MODE_PRIVATE constant, so they can't be read by others.
To achieve this, you'll need to do the following:
1) Request Root Access in your app (Having a rooted phone is NOT enough). (A library like RootTools can help you do this)
2) After you get root access, then you have to read the raw .xml file from the file system, and parse it accordingly. Then you can read all the data, and even write to it if you wanted.
Sounds a bit like you're confusing having a rooted phone and your app having root access, those are different things.

Custom password encrypted file system for storing files in Android

is it possible to create a custom file system or use an existing file system like EncFs to read data inside Android environment.
My idea is to make a virtual filesystem for storing some media files and developing an app which can directly access those files from the filesystem volume.
I hope im clear with my question.!!
Yes, but you need a rooted devices to use it. Or, you need to build your own firmware. Here's one, it's open source: http://nemesis2.qx.net/pages/LUKSManager

Open and edit file in .jar with Java?

How would you go about opening an .xml file that is within a .jar and edit it?
I know that you can do...
InputStream myStream = this.getClass().getResourceAsStream("xmlData.xml");
But how would you open the xmlData.xml, edit the file, and save it in the .jar? I would find this useful to know and don't want to edit a file outside of the .jar... and the application needs to stay running the entire time!
Thank you!
Jar files are just .zip files with different file suffix, and naming convention for contents. So use classes from under java.util.zip to read and/or write contents.
Modifying contents is not guaranteed (or even likely) to effect running system, as class loader may cache contents as it sees fit.
So it might be good to know more about what you are actually trying to achieve with this. Modifying contents of a jar on-the-fly sounds like complicated and error-prone approach...
If you app. has a GUI and you have access to a web site/server, JWS might be the answer. The JNLP API that is available to JWS apps. provides services such as the PersistenceService. Here is a small demo. of the PersistenceService.
The idea would be to check for the XML in the JWS persistence store. If it is not there, write it there, otherwise use the cached version. If it changes, write a new version to the store.
The demo. writes to the store at shut-down, and reads at start-up. But there is no reason it could not be called by a menu item, timer etc.

Categories