I have FileOpener
class for open documents by URL:
public class FileOpener {
private FileOpener() {
}
private static String url;
private static Uri uri;
private static Intent intent;
public static void open(Context context, FileDTO fileDTO) {
fileDTO.setUrl("https://calibre-ebook.com/downloads/demos/demo.docx");
setupBase(fileDTO);
checkFileTypeByUrl();
try {
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
private static void checkFileTypeByUrl() {
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
intent.setDataAndType(uri, mimeType);
}
private static void setupBase(FileDTO fileDTO) {
url = fileDTO.getUrl();
uri = Uri.parse(url);
intent = new Intent(Intent.ACTION_VIEW, uri);
}
}
In activity/fragment it's look like this:
@Override
public void openOrDownloadFile(FileDTO file) {
FileOpener.open(getContext(), file);
}
It's work fine for PDF/Txt
documents, but for MsWord/MsExcel/MsPowerPoint
have ActivityNotFoundException
(I have word/excel/powerpoint
on my device, which opening from other applications without problem)
Stacktrace:
W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://calibre-ebook.com/downloads/demos/demo.docx typ=application/vnd.openxmlformats-officedocument.wordprocessingml.document flg=0x4000000 }
What could be the problem?