Error in displaying different files on my Android App

79 views Asked by At

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);
}
1

There are 1 answers

0
SaTech On BEST ANSWER

I could find the solution myself and here is the snippet of the code I've used

private void openFile(File currentFile, String fileFormat) {
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(currentFile),fileFormat);
    startActivity(intent);
}

and I called this method as following:

...........

                } else if (currentFile.toString().contains(".pdf"))
                openFile(currentFile,"application/pdf");

             else if (currentFile.toString().contains(".doc") || currentFile.toString().contains(".docx"))
                openFile(currentFile,"application/msword");

             else if (currentFile.toString().contains(".jpg") ||
                    currentFile.toString().contains(".jpeg") || currentFile.toString().contains(".png"))
                openFile(currentFile,"image/jpeg");
             else if(currentFile.toString().contains(".ppt") || currentFile.toString().contains(".pptx"))
                 // Powerpoint file
                openFile(currentFile,"application/vnd.ms-powerpoint");
             else if(currentFile.toString().contains(".xls") || currentFile.toString().contains(".xlsx"))
                // Excel file
                openFile(currentFile, "application/vnd.ms-excel");
              else if(currentFile.toString().contains(".zip") || currentFile.toString().contains(".rar"))
                // WAV audio file
                openFile(currentFile,"application/x-wav");

             else if(currentFile.toString().contains(".rtf"))
                // RTF file
                openFile(currentFile,"application/rtf");

             else if(currentFile.toString().contains(".wav") || currentFile.toString().contains(".mp3"))
                // WAV audio file
                openFile(currentFile,"audio/x-wav");

             else if(currentFile.toString().contains(".gif"))
                // GIF file
                openFile(currentFile,"image/gif");
             else if(currentFile.toString().contains(".txt"))
                // Text file
                openFile(currentFile,"text/plain");