Skip to content

Commit

Permalink
Merge pull request #6216 from salachi/TIMOB-17567
Browse files Browse the repository at this point in the history
[TIMOB-17567] Android: Replace the use of getFileExtensionFromUrl as it is for enco...
  • Loading branch information
hieupham007 committed Nov 22, 2014
2 parents 035b211 + bb08ce1 commit 0c5c10f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
45 changes: 45 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,51 @@ public String guessContentTypeFromStream()
if (is != null) {
try {
mt = URLConnection.guessContentTypeFromStream(is);
if (mt == null) {
mt = guessAdditionalContentTypeFromStream(is);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e, Log.DEBUG_MODE);
}
}
return mt;
}

/**
* Check for additional content type reading first few characters from the given input stream.
*
* @return the guessed MIME-type or null if the type could not be determined.
*/
private String guessAdditionalContentTypeFromStream(InputStream is)
{
String mt = null;

if (is != null) {
try {

// Look ahead up to 64 bytes for the longest encoded header
is.mark(64);
byte[] bytes = new byte[64];
int length = is.read(bytes);
is.reset();
if (length == -1) {
return null;
}
if (bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F' && bytes[3] == '8') {
mt = "image/gif";
} else if (bytes[0] == (byte) 0x89 && bytes[1] == (byte) 0x50 && bytes[2] == (byte) 0x4E
&& bytes[3] == (byte) 0x47 && bytes[4] == (byte) 0x0D && bytes[5] == (byte) 0x0A
&& bytes[6] == (byte) 0x1A && bytes[7] == (byte) 0x0A) {
mt = "image/png";
} else if (bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xD8 && bytes[2] == (byte) 0xFF) {
if ((bytes[3] == (byte) 0xE0)
|| (bytes[3] == (byte) 0xE1 && bytes[6] == 'E' && bytes[7] == 'x' && bytes[8] == 'i'
&& bytes[9] == 'f' && bytes[10] == 0)) {
mt = "image/jpeg";
} else if (bytes[3] == (byte) 0xEE) {
mt = "image/jpg";
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e, Log.DEBUG_MODE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public static String getMimeTypeFromFileExtension(String extension, String defau

public static String getMimeType(String url, String defaultType)
{
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
String extension = "";
int pos = url.lastIndexOf('.');
if (pos > 0) {
extension = url.substring(pos + 1);
}
return getMimeTypeFromFileExtension(extension, defaultType);
}

Expand Down

0 comments on commit 0c5c10f

Please sign in to comment.