I'm trying to display different types of files on my android app, but it worked only with PDF files and when I try to display any other type of files. I get an error saying:
Can't display pdf, invalid file format
Here is a snippet of my code:
else if (currentFile.toString().contains(".pdf")) {
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(".PDF");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(currentFile), mime);
startActivityForResult(intent, 10);
} else if (currentFile.toString().contains(".doc")||currentFile.toString().contains(".docx")) {
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(".doc");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(currentFile), mime);
startActivityForResult(intent, 10);
} else if (currentFile.toString().contains(".jpg") || currentFile.toString().contains(".jpeg") || currentFile.toString().contains(".png")){
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(".jpg");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(currentFile), mime);
startActivityForResult(intent, 10);
}
I could find the solution myself and here is the snippet of the code I've used
and I called this method as following: