I am trying to access a file from the context, but I am unable to do this. I use context.openFileInput() method but it returns a stream while I need a File object of the file that i want to access.
Yours,
You can use below function of context which will return File
public File getFileStreamPath (String name)
Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.
try using this context.getFileStreamPath(String name)
for more details here
show one or two line of code -- since you profile shows engineer what Java does is about everything is an "Object" but you can get typing information with getClass().getName()
to get a File object on Android you use abstract File getDir(String name, int mode) in context but when you say stream it sounds like you are getting something of the general nature of a file ...
context.getFileStreamPath(fileName); will help you. "fileName" should be String type.
Related
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 :)
In C# there are Application.StartUpPath or Application.AppDataPath global methods to get a path where I would store Application Settings.
I tried static String fName = System.getProperty("user.dir") + "SatCli.conf";
"/SatCli.conf" - is the resulted fName
then I call
BufferedOutputStream oustream = new BufferedOutputStream(new
FileOutputStream(fName, false));
and I am getting the common exception
"/SatCli.conf: open failed: EROFS (Read-only file system)"
Well, I've been writing apps under Unix and I understand maybe the file name is refering to internal memory.. in other words to the root part of system file system.
Or maybe not.. maybe it refers to the App Folder?
Anyway, what I would like to get is the correct method to get the right folder to store the settings data.
Also, what is important, I need it before any context is built, when static fields are initializing.
Thank you for any relevant hint and help.
what I would like to get is the correct method to get the right folder to store the settings data.
For arbitrary files, call getFilesDir() on a Context (e.g., Activity). This gives you a File object representing a private directory where your app can read and write.
FWIW, Android's standard way of storing settings that you collect from the user is SharedPreferences, so you can take advantage of PreferenceFragment for the UI.
I need to get all txt files created by some installed application in android.
like if someone have skype installed so I need to get all text file created by this application.
For getting the file with particular extension like (.txt) you will have to use following code:-
class NameFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".txt") ||name.endsWith(".TXT"));
}
}
if it's your application, you can do that easily..
(you know the path you are using very well)
however, if it's other's app (like Skype), as you know, it writes data in it's own exclusive directory (in phone memory).. e.g., /data/data/com.skype.raider
you can still have control over this by checking files inside getFilesDir() - as long as you know the full-package-name (the principle is the same as Clear Data)
check this out for further reference..
but it may also write files in sdcard,. which (the path) might be unpredictable..
Using Eclipse, Android SDK.
I have a text file full of data that I need pulled in. (For now, it's the easier the way, eventually I'm going to need to scrape dynamic data from a URL, but for now I have the test data I need in this text file).
I've created a class to open this file, but no matter how I try to open it I keep geeting "file not found" exceptions.
I've tried putting my "data.txt" file in various relative paths (within my App):
- "/AppName/"
- "/AppName/src/com/example/appname/data.txt"
I've tried passing different relative paths. I've tried putting the text file in the same path of the .java class file that's trying to open it, and it still can't find it! What am I doing wrong?
What am I doing wrong?
You have two main options of where to store this file within your project directory: assets/ and res/raw/.
If you use assets/, you can call getAssets() on your Activity (or other Context), and on there call open() with the relative path within assets/ to get an InputStream on this file (e.g., assets/data.txt would be accessed via getAssets().open("data.txt")).
If you use res/raw/, you can call getResources() on your Activity (or other Context), and on there call openRawResource(), passing in the R.raw value based upon your filename (e.g., res/raw/data.txt would be accessed via getResources().openRawResource(R.raw.data)).
Use /res/raw directory. I read sound files from raw:
InputStream soundFile1 = context.getResources().openRawResource(soundOneId);
Where context is a base context of activity passed to the class.
http://developer.android.com/reference/android/content/Context.html
In Reference to this android file download problem
Can anyone explain what does this line mean in the code
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
And what does it mean by the parameter root within the File().
Do I need to specify the root path to save the file?
If it is the case then how do we specify the root path in android ?
Regards
And what does it mean by the parameter root within the File(). Do I need to specify the root path to save the file? if it is the case then how do we specify the root path in android?
The code snippet from the question you linked doesn't define the variable, but if the code is downloading a file to the device, I would assume that it's a path on the SD card. Environment.getExternalStorageDirectory() will give you the root path to the SD card. You'll also need to specify the WRITE_EXTERNAL_STORAGE permission in your manifest.
If you're working on the emulator, you can create a virtual SD card when you create the emulator image.
The java.io.File(File, String) or java.io.File(String, String) are standard java constructors for Java. The first argument is just the parent directory path, while the second is the actual file name. If the file is in the current working directory or you know the full path as one string you can avoid the 2 argument constructors.
Since you are trying to download a file you can just acquire the file through a normal URL.openStream() to get an InputStream to get the contents of your downloaded file. For writing the data out you will follow the example you linked to to write the contents.
I'm unsure what the root variable was pointed to in the example. I'm not able to help you beyond this though since I have only gone through the first Hello, Android example myself.