Unable to create a folder just after deletion in android - android

I am unable to create a folder in the same location with the same name just after deleting the folder with the same name(manually,deletion is manually,not programmatically). I am trying to create folder using
mediaStorageDir.mkdirs()
I dont know why this happens, also some times I see a junk file of size 0KB with the same name after I have deleted the file, I assume this is preventing the new folder creation. Those who have an insight into folder structure, pleas throw some light into this topic.
Below is the image of the dummy file that is created after the deletion.

Related

How to manually add a folder containing needed data in flutter

Iam working on a quiz project in flutter. I stored my quiz text files in folders according to the quiz category. And all these folders are stored in one root folder. So I should be able to list all the folders in this root folder ( which are categories ). And when category folder is opened the files in that corresponding folder should be listed.
The goal is to make buttons with each folder's name and the button will hold the path to open that folder.
ie, if the user clicks a button for folder, a list will be made containing the names of folders inside the clicked folder and according to this list a listview of buttons will be made.and finally when you open folder containing quiz files same will happen but button will contain the corresponding file path.
I have made the folder tree and functions to make buttons according to the names stored in list. But I have no idea where to put this folder inside the project.
I have tried puttin in the root of project, in Lib folder, in asset folder but flutter is failing to detect this directory.
I really need this to work for my project to go forward. Any help of pointers will be appreciated.
For this to work you need multiple things:
Viewing you folders and text files within flutter
Listing your folders and text files
Reading your text files
1 - Viewing you folders and text files within flutter
Since the assets notations doesn't support recursion, you have to either:
Write everything by hand
Use a plugin to generate the assets for you
Since you have many file I suppose the second option is the best for you. I did struggle to find one that worked but FlutterAssetSync is working like a charm according to my tests. The only constrain is that you have to put everything inside the "assets" folder (which is not really a constraint tbh).
Once you put you file in the assets folder, execute the plugin (under Tools > FlutterAssetSync). This will automatically add everything to your pubspec.yaml
2 - Listing your folders and text files
In order to get the name of your folders and your text files you can use the answers to this stackoverflow question. Basically here is the implementation:
Future<List> _getImages(String folderName) async {
final manifestContent = await rootBundle.loadString('AssetManifest.json');
final Map<String, dynamic> manifestMap = json.decode(manifestContent);
return manifestMap.keys
.where((String key) => key.contains(folderPath))
.toList();
}
3 - Reading your text files
To read the content of a text file asset you must use loadString (as we did in _getImages btw). Here is a quick implementation:
Future<String> _getQuizzText(String quizzTextPath) async {
return await rootBundle.loadString(quizzTextPath);
}
Putting everything together you should be able to make it work!

How to add two files with the same file name into a single zip archive file?

As from the zip format specification http://en.wikipedia.org/wiki/Zip, it is possible to add two files with the same file name into a single zip archive file.
For example, I'd make a zip looks like
foo.zip
--bar.txt
--bar.txt
--3rd.txt
Does anyone know to accomplish this?
I tried linux utility zip and unzip, it always overwrites the previous added zip entry.
The Java class java.util.zip.ZipFile does not work either.
One way to do this is to create the zip initially using unique names - e.g. bar.txt, car.txt and 3rd.txt. Then open up the resulting zip file in a binary editor and search for car.txt and replace it with bar.txt.
Note that there should be two occurrences of the filename that you need to replace - one in the local file header for the file (somewhere in the middle of the zip), and one in the central directory (somewhere near the end of the zip).
If you need to do this programmatically, I would suggest you actually parse the central directory to find the exact positions of the filenames in the various headers rather than a simple search and replace to avoid the chance of false positive. It's not a very complicated format.
Note that when you try to uncompress a zip like this, you may get a warning about the file already existing when the second copy of the filename is uncompressed, depending on what program you are using for unzipping and what options you have set.

Android: Writing to XML file in Assets

I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)

Add text file resources to R.java

I have text files named hierarchy1.txt, hierarchy1.1.txt and heirarchy1.1.1.txt in my android project->res->raw folder. These files have json objects. I m trying to read from these files but these are not getting added in my R.file. How do I resolve this?
You should use underscores instead of dots in your filenames.
If you rename your file to hierarchy1_1.txt you should be able to access it with:
context.getResources().openRawResource(R.raw.hierarchy1_1);

custom Resource folder names for layout,drawable .. in android

I have 2 different functionality to be implemented as part of a single application.
They have different screen flows and BL to be implemented but a single apk,so it is transparent to the end user.
I have 100 screens for each of the flow,so can I have custom folder names in the resources directory for easy maintenance and loose coupling.
eg: res
-layout
-layout_savings
-layout_checking
-drawable
-drawable_savings
-drawable_cheking
currently we have only the following structure
res
-layout
-layout-land
-drawable
-values
Will there be any problem while generating the R.java file.
Any help would be appreciated.
for what you need , i suggest you to use the assets directory instead of res. in assets dir you can sort your files anyway you like (dir name/filename) . You access them with AssetManager.
this require a little change in code but i think this is the only way if you want to sort your file like that , res folder doesn't allow custom directory names.
(Or of course you can store files in the sdcard)

Categories

Resources