Skip to content

Commit 4b100ad

Browse files
committed
Transfer: guess mime type
Some devices reject an empty mime type when saving to a directory managed by the Downloads provider
1 parent 661f712 commit 4b100ad

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

app/src/main/java/slowscript/warpinator/Transfer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.FileNotFoundException;
1818
import java.io.InputStream;
1919
import java.io.OutputStream;
20+
import java.net.URLConnection;
2021
import java.util.ArrayList;
2122
import java.util.List;
2223

@@ -279,7 +280,8 @@ else if (chunk.getFileType() == FileType.SYMLINK) {
279280
parent = DocumentFile.fromTreeUri(svc, dirUri);
280281
}
281282
//Create file
282-
DocumentFile file = parent.createFile("", fileName);
283+
String mime = guessMimeType(fileName);
284+
DocumentFile file = parent.createFile(mime, fileName);
283285
currentUri = file.getUri();
284286
currentStream = svc.getContentResolver().openOutputStream(currentUri);
285287
currentStream.write(chunk.getChunk().toByteArray());
@@ -389,4 +391,13 @@ private void createDirectories(DocumentFile parent, Uri rootUri, String path, @N
389391
if (rest != null)
390392
createDirectories(newDir, rootUri, rest, absDir);
391393
}
394+
395+
private String guessMimeType(String name) {
396+
//We don't care about knowing the EXACT mime type
397+
//This is only to prevent fail on some devices that reject empty mime type
398+
String mime = URLConnection.guessContentTypeFromName(name);
399+
if (mime == null)
400+
mime = "application/octet-stream";
401+
return mime;
402+
}
392403
}

0 commit comments

Comments
 (0)