Skip to content Skip to sidebar Skip to footer

Android Intent Filer For Opening Pdf Files

I added my pdf viewer app to 'Complete action using' list for pdf files. But if I run a file on the list, the app just shut down. I made intent filter in onCreate method and manife

Solution 1:

Try this

  //Method to generate a MIME Type
  private static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

 //Method to generate File URI
 private static Uri getFileUri(String filePath) {
    Uri fileUri = null;
    File file = new File(filePath);
    try {
        if (Build.VERSION.SDK_INT >= 24) {
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }
        fileUri = Uri.fromFile(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fileUri;
}


 //Intent
 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(getFileUri(file_path), getMimeType(file_path));
 startActivity(intent);

Post a Comment for "Android Intent Filer For Opening Pdf Files"