Tech Blog

Android

Android Download file with Data URL

By Bansari Pandit1 min read
← Back to blog

What is a Data URL?

Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type.

data:[<mediatype>][;base64],<data>

To download files having Data URLs inside Android:

Firstly, we need to get an extension of the file from its data URL. To do the same, use following code:

Pattern pattern = Pattern.compile("(?<=data:)(.*)(?=;)");
Matcher matcher = pattern.matcher(myDataUrl);
if (matcher.find())
{
   Logger.logInfo("DECODE : "+ matcher.group(0));
   String extractedMimeType = matcher.group(0);
   extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(extractedMimeType);
   if (Util.isNullOrEmpty(extension)){
       if (extractedMimeType.contains("text/csv")){
           extension="csv";
       }else if (extractedMimeType.contains("text/plain")){
           extension="txt";
       }else if (extractedMimeType.contains("text/html")) {
           extension="html";
       }else if (extractedMimeType.contains("application/pdf")){
           extension="pdf";
       }
   }
}else {
   extension="txt";
}

Then, we need to create one file in device storage with the required extension: 

File destinationFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"download1."+extension);
  1. UrlDecoder
Example : data:Hello%20World%21

String source = URLDecoder.decode(myDataUrl).replaceAll("data:.+?,","");
BufferedOutputStream output = null;
output = new BufferedOutputStream(new FileOutputStream(destinationFile));
output.write(source.getBytes());
output.flush();
output.close();

In DataURL, the Base64 string is optional but if the URL contains Base64 encoded data, it will not be decoded and converted to a file using URLDecoder. In this case, you need to use Base64 decoding as below.

  1. Base64 decode
Example : data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==

byte[] txtAsBytes = Base64.decode(myDataUrl.split(",")[1], 0);
FileOutputStream os = new FileOutputStream(destinationFile, false);
os.write(txtAsBytes);
os.flush();
os.close();

Keep reading

You may be interested in

Browse all articles →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →
How to avoid detach of Android Debugger while debugging Android Process?
November 28, 20222 min read

How to avoid detach of Android Debugger while debugging Android Process?

OVERVIEW: Recently, I came across this problem many times where I attached the debugger to the Android Process, debug breakpoints were hit and I started evaluating some values or doing step over or step into debugging and suddenly, App isn’t responding dialog comes and debugger detaches from the process. To avoid detaching of the debugger […]

By Ankur AgrawalRead More →