read image from assets folder / sub package android studio kotlin - android

I have a situation where I need to read an image file (jpg) in a submodule.
The class where I need access to that image is a normal class. So I have two options.
I put that image in the asset folder and try to use assetManager, which doesn't work because I don't have context. My project doesn't have any activity or fragment, because it's a submodule
I put that image in a package. How can I access that image?
The code was written in kotlin.
I will be appreciated any help or suggestions

Related

Accessing assets of main application inside package

i'm currently trying to develop a package for a Flutter App, with Kotlin. My issue is that I need to provide the package with a config file, which should only be defined inside the main App. Since the config differs for the Dev and Prod environment, the app should pass through the path of the File via the Method Channel. The problem is that the package isn't able to access the assets folder of the calling application.
Path: "assets/config.json" (the root being the main application)
Steps I already tried:
Creating the file inside the res/raw & accessing the config file through a ressource id -> Kotlin gives me an "Unresolved reference" error, unless I create the file inside the packages res/raw
Instead of passing through the path, I tried passing through the content of the config & writing it into an empty temporary file. The code in Kotlin like this:
val config = File(applicationContext.filesDir,"config.json")
config.writeText(configContent)
-> This works, but it seems like a weird solution to the problem.
please let me know if I need to provide further information & thank you in advance!
edit:
The Java Method that is called during initialisation:
public static void createMultipleAccountPublicClientApplication(#NonNull final Context context,
#NonNull final File configFile,
#NonNull final IMultipleAccountApplicationCreatedListener listener)
Flutter assets aren't files - they are packaged up and only available through the rootBundle. So, if you want to make a file from a text asset, someone has to load the asset and write it to a file.
As your plugin user will be in charge of the asset, they will have to do the first part (and will end up with a String). The question arises of who should do the writing.
You could make the plugin user use path_provider to find the temporary directory and write it there and then pass you the file path. Eventually, down in the Java, you new File(theTempFilePath). Or they could pass the string to the Dart half of your plugin and you create the temp file in the same way.
It's probably more convenient if they pass your plugin the string, you pass that to the native side and have the native side create a temporary file and write the string there. (BTW, I assume we are talking about this config file: https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-configuration#how-to-use-a-configuration-file )
See this answer for creating temporary files: Creating temporary files in Android
Note that there's actually no reason that your plugin user then needs to use an asset. They could, instead, just hard code the string in their code if the configuration never really changes.
There's an argument that as this is a JSON configuration file, you may not want to bother your user with the details of this JSON configuration file. You may want to default it in your Dart code (why not hard code it as a string, as above, if it never really changes) and then provide some methods to override particular values like the client id and the redirect uri, which may be the only things that users ever change in practice. So rather than making them supply a complete JSON file, they just give you those two strings and you plonk them into your default JSON. Maybe a version 2 feature :)

Add XML Serialized object to APK so it is copied into the app's private files directory

I have a desktop application that produces resource / data files for my android app. These are XML text files that store instances of my custom data class. These objects are serialized using the Simple XML Serialization library. In my android app, I'd like to instantiate objects from this XML serialization class.
I like to add these xml files to Android Studio so they are included in the APK on device install and are placed, for example, in the private app directory "files", to which getFilesDir() is mapped. I can't find a way to do that.
If I add these xml files to the Android XML resource folder, I need to use Android's XML resource parser, and can not use the Simple XML library.
Any tips? I feel I made a wrong design choice seeing how restrictive the resource bundling is.
Thanks, Kind regards,
Harmen
As per CommonsWare's comment: the solution was adding it to the raw resource folder, then you can access it using:
InputStream xmlExerciseInputStream = getResources().openRawResource(R.raw.myresource);
MyClass myClass = serializer.read(MyClass.class, xmlExerciseInputStream);

How to create a folder programmatically in Android

I wonder how you (C#)programmatically create a folder in the Assets Folder in Android.
My project is called App1 so the folder I want to create is named: "imagefolder1"
The full path look like this:
App1/Assets/imagefolder1
How can we create "imagefolder1" here?
(Notice that I want to have many image folders in the Assets Folder. Please tell if this is the wrong place to have them)
I wonder how you (C#)programatically create a folder in the Assets Folder in Android
You cannot do this, in any programming language. Assets are not files on the device. They are packaged in the APK, and you cannot modify them, including creating directories.
var pathToNewFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Assets/imagefolder1";
Directory.CreateDirectory(pathToNewFolder);

Not able to access image in drawable from non-activity class in Android

I have created an Android library project that i am linking with my Android app. In the drawable folder of the library project, I have added a few images. Now I am trying to access these images from a non-activity class in the same project but they are not being accessed when I try to access them using the following snippet
Drawable drawable=context.getResources().getDrawable(R.drawable.image);
The import of R file is correct and the image id is being generated correctly in the R file. The context is correct. I tried refreshing and cleaning/building the project multiple times but nothing seems to work.
Help!
[Note: Answer copied from here.]
Yes you can if you know the package name of your library.
Resources.getIdentifier(...)
You can do:
getResources().getIdentifier("res_name", "res_type", "com.library.package");
ex:
R.id.settings would be:
getResources().getIdentifier("settings", "id", "com.library.package");
I'm not sure I understand the quest completely (the problem you are having is that the method does not return a drawable?
I'm not sure you about how you are getting the context, but you could try this:
Drawable drawable = ContextCompat(getContext(), R.drawable.image);
If you are storing the context in a variable, you can pass the variable instead of using getContext().
Additionally, the method you are using to get the drawable is deprected, so you shouldn't be using it anymore.
Let me know if it helped you and remember to upvote/select this as correct answer if it did, cheers.

Where do I put haarcascade_frontalface_alt.xml in the Android sample of JavaCV's FacePreview class?

There's a snippet of code in the Android sample's FacePreview class that boggles me:
File classifierFile = Loader.extractResource(getClass(),
"/org/bytedeco/javacv/facepreview/haarcascade_frontalface_alt.xml",
context.getCacheDir(), "classifier", ".xml");
I know I should change the names before haarcascade_frontalface_alt.xml to match my project name, but I can't figure out where to actually put the XML file in my project structure.
I did read somewhere that I have to put the xml file in the same folder as the FacePreview class... so does that mean I have to put it in src/main/java/<package_name>?
Edit: That doesn't work either. Do I have to download and cache it in the Activity?
it goes in src/main/resources/org/bytedeco/javacv/facepreview/haarcascade_frontalface_alt.xml
assuming gradle does the right thing

Categories

Resources