Checking If Folder Name is valid - android

I am creating a folder in android's internal storage by using the below code-
File file=new File(this.getFilesDir()+"/"+FolderNamefromEditTextasString);
file.mkdir();
I am getting the folder name from an Edit Text. I want to know how to check if the folder name is valid or not before creating. I don't exactly know which characters should not be there in a valid folder name. I first would like to validate the string from edit text. Also, I don't like to have spaces and "." in folder name string.

use regular expressions to test if it is something valid. I guess you can allow numbers, letters - and it can be ok not to allow special characters (such as ë,# and other)
something like [0-9a-zA-Z/-+]+ should work just fine

Related

Unable to download file with special character from Amazon S3

I have been trying to download a file from Amazon S3 that ends with special character.
The file name ends with an "=" as a result of Base64 encoding. Now I am trying to download this file and I receive an error,
The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey;
I tried URL Encoding the string. So now the "=" becomes "%3D" and still I receive the same error.
But if I remove the "=" from the file name, I am being able to download the file without issues. But this is a common file and it is to be accessed form iOS as well.
NOTE: The iOS Amazon SDK works even when the file name has "=" in it.
The issue is faced only in Android SDK.
According to AWS documentation
Safe Characters
The following character sets are generally safe for use in key names:
Alphanumeric characters [0-9a-zA-Z]
Special characters !, -, _, ., *, ', (, and )
and
Characters That Might Require Special Handling
The following characters in a key name may require additional code handling and will likely need to be URL encoded or referenced as HEX. Some of these are non-printable characters and your browser may not handle them, which will also require special handling:
Ampersand ("&")
Dollar ("$")
ASCII character ranges 00–1F hex (0–31 decimal) and 7F (127 decimal.)
'At' symbol ("#")
Equals ("=")
Semicolon (";")
Colon (":")
Plus ("+")
Space – Significant sequences of spaces may be lost in some uses (especially multiple spaces)
Comma (",")
Question mark ("?")
So it confirms you that "=" require special handling,
It will be better if you replace the last "=" char with another safe char to avoid the issue ...
Please try to change the "=" to "&#61"
As on iOS, there is no issue, I expect that it could be relative to the Android environment.
You may note that some chars could also be forbidden because the SH or BASH or ANDROID shell environment execution,
please also to take in consideration that some disk format option (FAT32 on a normal android external memory card) could also represent a factor which forbids some char in the filename.
If you take a look here and more especially on the #kreker answer:
According to wiki and assuming that you are using external data storage which has FAT32.
Allowable characters in directory entries
are
Any byte except for values 0-31, 127 (DEL) and: " * / : < > ? \ | + , . ; = [] (lowcase a-z are stored as A-Z). With VFAT LFN any Unicode except NUL
You will note that = is not an allowed char on the android FAT32 partition ...
As I expect that Android will consider = as restricted char you may try to escape it with a \= or add a quote to the file name on your code ...
An example with a copy:
cp filename=.png mynewfile=.png #before
cp "filename=.png" "mynewfile=.png" #after
"VCS...=.png"
If nothing of this tricks will work you have to change the filename to remove the "=" when you create those files.
Regards
The following characters in a key name may requiere additional code
handling and will likely need to be URL encoded or referenced as
HEX.
Some of these are non-printable characters and your browser may not handle them, which will also require special handling:
The best practices to ensure compatibility between applications defining Key names are using:
- Alphanumeric characters [0-9a-zA-Z]
- Special characters !, -, _, ., *, ', (, and )
Using android you need to encode the file name, the character (commonly used as operator):
=
to :
%3D
First of all I think you are using CopyObjects method of s3. OR you received a file name from an s3 event or somewhere else which you are trying to download. The issue is aws handles special characters differently when they store the names. If you'll go to s3 console and click on the file name. You'll see the URI which will have different values for special characters like space will be replaced by + like that. So you need to handle the special characters accordingly. Misleading examples wont help you as aws has constraints on file names but if you save them otherwise it will replace them with acceptable characters and your actual file name will be different than the one you uploaded hence you getting file not found

Android Studio:Where to store a text file and the path for it

I'm developing a strategy game that will have a country full of kingdoms. I want to be able to store and read back in the information of the kingdomgs. I've looked at tutorials online but they just aren't specific to what I'm looking for. So basically:
-Where to store a text file which holds string values.
-The correct file path.
-And how to read from that text file, and check if it is empty.
You can store text data in string resource file:
https://developer.android.com/guide/topics/resources/string-resource.html
Also you can use asset to store text
https://developer.android.com/reference/android/content/res/AssetManager.html
And there is diffrenece:
Difference between /res and /assets directories

no resource found that matches the given name at hint = #string/edit_message

I am new to android, I am trying to create a sample UI which is described in the developer.android website. When I add a edit text, I am getting that error message.
If you are currently using this: android:hint = "#string/edit_message", just do like that: android:hint = "edit_message". This is called hardcoded string. Its working, but its not recommended that you make strings this way. The other way is to go to resources, then to strings folder, and there should be one .xml, with strings. To make a new string, just copy the last one, change the name, and the content between >...<, where your string should be. To use that string you get it by its name. So if you name it "myStr", to use it in editText, you need to say: android:hint="#string/myStr" and you will get the content of the string. Hope this helps. Mark as answer if it does. Good luck.
Did you add the string resource?? To the left is package explorer. Select Your project and go to its res folder. In res folder select values and then click on strings.xml. Add your string resource there and save it. Then I dont think theres scope for such error messages.

Android - lookup an xml element node name from its value

I have an android app that I want to internationalise.
I have extracted the app strings and deployed them in resource files and all that works fine.
The remaining issue I have is that my app reads a folder structure and actually pulls filenames in as words to use in the app.
I have these filenames/words defined in my xml, but I can't figure out how to dynamically lookup the english language word.
So. here's the scenario.
Filename = hello.png. I want the word "hello" to appear in my app corresponding to the image; I have the word "hello" defined in my strings.xml and the corresponding language files as "hello_file" (i.e. the word "hello" can be accessed by R.string.hello_file). What I think I need to do is take the english word from the filename and do a reverse lookup on the strings.xml file and find the node corresponding to that and then lookup the corresponding word in the strings_xx.xml file for the iso language translations.
But I don't know how to do that...
Perhaps I'm over complicating this? It does not seem an ideal use case for the strings_xx.xml translation facility.
Any other ideas?
Use string array http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
String[] files = getResources().getStringArray(R.array.file_names);
In code you can loop through all values to find which one you need.

Renaming directory gives positive result code, but didn't rename

I know it's only cosmetic but the below code should rename my directory however it doesn't. The difference is just some capitalisation - but afaik Android is fully case sensitive when it comes to filenames. Like Linux normally is too.
The rename gives a true result, indicating the operation was successful. However the directory in question is NOT renamed, and it still has two capital D's.
I have previously used the same code to rename from /DeadDropDroid to /.DeadDropDroid and that works fine. Every time I run the below code the log says "success".
oldBasePath = new File (Environment.getExternalStorageDirectory()+ "/.DeadDropDroid/");
if (oldBasePath.exists()) {
if (oldBasePath.renameTo(new File(Environment.getExternalStorageDirectory()+ "/.DeaddropDroid/")))
Log.v(TAG, "Rename success.");
else
Log.v(TAG, "Rename fail.");
}
Have a look at this answer to a similar question. The key content is
By default, the SD card is formatted as FAT, which preserves case but is case insensitive.
I did some checks and can confirm that the mentioned File methods work 'case insensitive' on the SD card. You even can not check oldBasePath.exists() as ist also will return trueif the LowerCase directory version exists instead of the UpperCase version. You have to read the directory content and compare the file/directory names you received with your pattern.
Renaming will also be a two step approach (via a temp File), e.g
.DeadDropDroid -> .DeadDropDroid_tmp -> .DeaddropDroid

Categories

Resources