I'm fetching byte array from server but don't know the exact extension of the file because that file could be .pdf / mp4 / docx / zip, so how would I know the exact extension after saving it to sdcard.
Any idea?
There's no way to reliably determine the file extension from the file contents. It's like trying to find out what was written on an envelope, if all you have is the letter itself.
You could guess, but that would be a difficult task (for example, a docx is also a zip file). There is specialized software for this (many operating systems include the file tool), and you might find a third-party library that you can use, but even if you do that, it's still a guess.
I'd rather concentrate on finding a way for the server to tell you the original file name in addition to the contents.
There is no proper way to determine the file extension right away.
However you can impose conditions for the specific file types like if the file extension is mp3 then do the certain task. For that you can use FilenameFilter.
Check the how-to-find-all-files-with-certain-extension-on-android for more information about it.
If it is custom protocol, you should pass one(or more) byte of data type to the client side, then you get data and add extension on the client side according to the first byte(s). If it is http-like protocol, you can add some header to the answer. But it is need to do as on the serverside as well as on the clientside too.
Related
I am currently creating an android library that uses json commands for communication with another library. I would like to extract these commands from code and save them in separate files for better readability.
Where do I save those files when there is no asset directory and how do I read them?
Edit: I have found an answer to my question:
how to access resources in a android library project
Have a look at Internal Storage. The files saved here are only available to your app. It uses the Java File API to read an write using the FileInputStream and FileOutputStream
UPDATE:
As per the discussion in the comments, OP was looking for a method to ship a JSON file with the library. In light of that:
I'm not sure if library modules support raw resources. If they do, you might want to use that but it will increase the size significantly. You could also fetch the file from a server the first time you're the library is used, keeping track of that using a SharedPreference entry.
I am trying to use the SAF picker in my application to look for my custom file types. Doing this is easy enough when the file has a mime type by calling setType("text/plain") or similar.
What would the call look like if I wanted to look for, say, all files with the '.blah' extension?
Edit: more information: the file type is actually a plain text file with a changed extension. In the file, some text exists which I parse to create some data structure from it. However, when the user looks for the file of the specific extension, I would like them to see only my custom files, not all text files.
I am not a 100 percent sure if this also applies to android (cause I have not used it until now), but in general the mimetype for all binary files can be 'application/octet-stream' if you don't know what type of file you are dealing with
I am new to Android. I want to know whether it is possible to read data from MS Excel file without using any jar files in android?
If it's possible then what's the best approach to do it .
Yes and no.
It's entirely possible to code a complete Excel API to read (and write) an Excel file. But that takes time and a lot of energy, as these formats are huge and complicated. You are practically mirroring the functions of Apache POI or JExcelApi. And these projects are huge.
As I don't know your reason for not using an external library (like an .jar) to read Excel, my answer is: Yes it's possible. Read the excel specification and implement an library to read this format. But practically: Don't do it and use the libraries from above. This will preserve you from a lot of pain.
There is one very nice feature in shell
for example
# "asd" > myfile.txt
puts "asd" in myfile.txt if the file exist first it is deleted then it is created and the content is put in the file.
but if
# "asd" >> myfile.txt
and if the file exist then the "asd" will be just added to the end of the file.
well I need some EFFICIENT algorithm that do exactly this.
I have very large text file and all I want to do is to write something to the end, but it must be very efficient I do not want to waste resources on stupid things like read the whole content concatenate and write...
I know the general concepts about files. One thing which is useful for your case is opening a file in append mode. If you open a file in append mode and trying to write data to it, it just append(adds at the end of file) the given data.
Try to search for similar functionality in android API. This is general feature of File System. In linux also we have familiar with this type of file operations.
I hope it may help you.
Is this what you are looking for.
I create an application in android.
I know .apk can convert into .zip and my layout and xml files are extracted.
Is there any way to avoid the decoding of xml?
How can i encode the apk so it cannot converted?
thanks
An .APK file is simply a .ZIP file with a predefined directory/file layout, there is no "conversion" (unless you're simply talking about changing the extension).
ProGuard can be used to obfuscate code, but there's no "standard" way of encoding/encrypting the XML files. You could encrypt/decrypt them manually yourself, but you'd be introducing a great amount of overhead at runtime for decryption, and many standard interfaces would not work because they expect the standard plan-text XML format. You're looking at a great deal of code to accomplish what you're asking. This is all assuming you're loking to encrypt your strings.xml, etc.
If you're looking to encrypt custom XML data files, there's a thread with some good suggestions here.