Android: Create correct file name from url - android

I download file from many URLs.
e.g.:
https://myhost/sh/8n0wli4v5895jom/AAB2E0WA2fetPTLjWtYe5HjAa/00019.jpg
https://myhost/sh/8n0wli4v5895jom/AAB2E0WA2fetPTLjWtYe5HjAa/0001.jpg
https://myhost2222/sh/8n0wli4v5895jom/AAB2E0WA2fetPTLjWtYe5HjAa/00019.jpg
https://myhost2222/sh/8n0wli4v5895jom/AAB2E0WA2fetPTLjWtYe5HjAa/0001.jpg
Now I need to save all download files to Android local folder.
But what is file name must be?
I think to create file name from URL.
But URL contain forbidden characters for file name. So... I thing to create hash (SHA-1) from url or maybe convert URL to Base64.
Is this a good solution?

The idea is to save the file with filename the same as the URL it was downloaded from. This way it will be easy to find it (reconstruct the filename) in the filesystem (cache) given the URL.
So suppose we have this URL: https://myhost/sh/8n0wli4v5895jom/AAB2E0WA2fetPTLjWtYe5HjAa/00019.jpg
Invalid characters are : and /, so a simple String.replaceAll will remove these characters
String filename = "https://myhost/sh/8n0wli4v5895jom/AAB2E0WA2fetPTLjWtYe5HjAa/00019.jpg".replaceAll(":\\/\\/|\\/", "");
This filename variable equals to: httpsmyhostsh8n0wli4v5895jomAAB2E0WA2fetPTLjWtYe5HjAa00019.jpg
Of course if you want you can instead of completely removing those characters, to replace them with a valid character e.g. -.

make names from time:
String carTime = String.valueOf(System.currentTimeMillis());
String fileName=carTime.substring(carTime.length() - 4, carTime.length())+".jpg"

I think a base64 is good solution if you want a reversed way.
URL => FILENAME (encode base64)
FILENAME => URL (decode base64)
If you just replace : and \ by nothing you lose information and can't
retreive the orginal URL from FILENAME
If you use a Hash fonction you lose information too (and you add a very very very small risk of collision)
Maybe today you don't need a reversed solution but tomorrow for debug you don't know ;-)
Be carrefull : Filenames have limit length
EDIT :
Another solution is to simply create intermediate directory (replacing only https:// by your local temp folder)

Related

Checking If Folder Name is valid

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

Access the file from salesforce

I am using salesforce for my android app in backend.
and somewhere while using SOQL, I am getting response something like this :
Article_FileNames":"Template.doc"
Article_FileContentTypes":"application/msword
Article_FileBodys":"/services/data/v35.0/sobjects/Hub_Knowledge_Articlekav/xxxxxxxxxxx/Article_FileBodys
ArticleNumber":"000001010
LastPublishedDate":"2015-12-09T20:34:48.000+0000
Now i am not getting, how I can let the user open the file named "Template.doc" on click.
How to open this file ?
is there any file path or id which will linkify this filename ?
Any help will be appreciated!
The only file types that are directly available from outside of your Salesforce instance are images, which are made so by setting the Externally Available Image flag on the Document object itself.
If you need to work with other file types, you're going to have to build them yourself to facilitate their download by the end user. This can be done by querying your instance for the Document, which will contain the Base64 encoded file contents (Body), that array's length (BodyLength), the file extension (Type), and also the file name itself (Name).
Given what you provided, your SOQL query would look something like :
SELECT Body, BodyLength, Name, Type FROM Document WHERE Name = 'Template' AND Type = 'doc'
From there, you should have everything you need to build the file in memory.

Get the file/image name from the URL which does not end with a file name and extension

My query is almost similar to one asked in the following link :How to get file name when clicking on URL in webview I don't want to use URLUtil for the reasons mentioned there. Also, getting the file name using Content-Disposition header field does not work in my case. I am using Android DownloadManager API for downloading file/images. I have to set destination filename using setDestinationInExternalPublicDir (String dirType, String subPath) method. I have observed that DownloadManager provide name such as "downloadfile.jpeg", "downloadfile-2.jpeg" when any image url is used. I have looked into the DownloadManager source code and found COLUMN_LOCAL_FILENAME and COLUMN_TITLE that might be storing the name of file. How can I get the file/image name as done in DownloadManager API ? If not, is there any way I can detect that these urls does not contain filename at the end so that I can provide my custom name ?

Android webview not opening sdcard path starting with special character

In android, i am trying to open an html file from sdcard path to show it in web view . As my path starts with some special character( eg. sdcard/11/#directoryName/index.html), it is not opening that html file . can any one help me to resolve this issue?
Thanks
The character "#" is not a legal character in an URL path. The "#" character is used to reference an "anchor" within a document. Usually used like this:
http://my.host.com/path/to/url#part-of-document
You could try to URL-encode that character in the path so that it doesn't get interpreted as an anchor reference, like this:
file:///storage/sdcard/11/%23directoryName/index.html
but it may be that you will need to read the file yourself and pass the data to the WebView instead.

Simple way to convert a URL into a filename

I'm writing an asynchronous image downloader for Android and was just wondering, given an arbitary URL such as:
http://www.android.com/images/brand/droid.gif
What would be the best way to convert the unique url to a filename. I thought about simply splitting the url and grabbing the last section, but I want the filename to be representative of the whole URL. The other alternatives I thought were replacing all the forward slashes with underscores or simply hashing the whole URL and storing this.
If anyone has any ideas I'd love to hear them!
Thanks
In case, usually uses MD5 hash. but I suggest to use 'aquery' library. In library you can simply download Image asynchronous and put it to view. It also support disk cache, memory cache simply.
This method will be fulfill your requirements. It will generate a name which will represent original URL. You can call generateNameFromUrl(String url) method like this.
String url = "http://www.android.com/images/brand/droid.gif";
String uniqueName = generateNameFromUrl(url));
Method is given below:
public static String generateNameFromUrl(String url){
// Replace useless chareacters with UNDERSCORE
String uniqueName = url.replace("://", "_").replace(".", "_").replace("/", "_");
// Replace last UNDERSCORE with a DOT
uniqueName = uniqueName.substring(0,uniqueName.lastIndexOf('_'))
+"."+uniqueName.substring(uniqueName.lastIndexOf('_')+1,uniqueName.length());
return uniqueName;
}
Input: "http://www.android.com/images/brand/droid.gif"
Output: "http_www_android_com_images_brand_droid.gif"

Categories

Resources