2

I have an app which displays results in webView. Results may include formats like .pdf, .ps, .dwf, .kml, .kmz, .xls, .ppt, .doc, .docx, .rtf and .swf
Now when user click on .pdf type link the I would like to display apps which can support pdf format like "google docs", "pdf reader" etc. Similarly, when user clicks any .swf file then apps which support swf files like "swf player"get listed in the intent.
I have download listener like this:

webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setDataAndType(Uri.parse(url).normalizeScheme(), Intent.normalizeMimeType(mimetype));
                startActivity(Intent.createChooser(intent, "Open link via..."));
            }
        });


But I didn't get expected results.
How to achieve this ?

I don't want to download the file first and then open it. Also, I avoid using this approach:
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://example.com/xyz.pdf");

r.bhardwaj
  • 1,603
  • 6
  • 28
  • 54

2 Answers2

3

I use something like that in my project, so just attach yout link/downloaded file, to it:

SharedMethods.openFile(getActivity(), new File(filePath));

and my SharedMethods.openFile:

public static void openFile(Context context, File url) throws IOException {
    Uri uri = Uri.fromFile(url);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
        intent.setDataAndType(uri, "application/msword");
    } else if(url.toString().contains(".pdf")) {
        intent.setDataAndType(uri, "application/pdf");
    } else if(url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if(url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if(url.toString().contains(".rtf")) {
        intent.setDataAndType(uri, "application/rtf");
    } else if(url.toString().contains(".gif")) {
        intent.setDataAndType(uri, "image/gif");
    } else if(url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) {
        intent.setDataAndType(uri, "image/jpeg");
    } else if(url.toString().contains(".txt")) {
        intent.setDataAndType(uri, "text/plain");
    } else if(url.toString().contains(".odt")) {
        intent.setDataAndType(uri, "application/vnd.oasis.opendocument.text");
    }  else if(url.toString().contains(".ods")) {
        intent.setDataAndType(uri, "application/vnd.oasis.opendocument.spreadsheet");
    }  else if(url.toString().contains(".wps")) {
        intent.setDataAndType(uri, "application/vnd.ms-works");
    } else {
        intent.setDataAndType(uri, "*/*");
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent);
}

Hope it helped.

bovquier
  • 78
  • 1
  • 14
  • I think this implementation will work only if we have pre-downloaded file(.doc, .pdf etc). Will this work if we have "web url" of these formats like "http://example.com/xyz.pdf" , "http://example.com/xyz.doc" etc – r.bhardwaj May 28 '14 at 12:38
  • 1
    if you get file extension from URL, or file type from response header, you can change my method and pass these data... anyway, I think, that you can open file in android only if you have downloaded it previously. – bovquier May 29 '14 at 07:52
  • Yeah I can extract the file type and even file name like **xyz.pdf** or **xyz.doc**, but still I don't think I can open it directly without downloading. Is there no other solution for this other than downloading these files prior to open them ? – r.bhardwaj May 29 '14 at 09:19
  • 1
    If apps on Android can download files from given URL, than it's possible, but for 3-4years as I use Android phones I didn't find out that possibility ;) – bovquier May 29 '14 at 10:25
0

You can filter result for list of applications, here is one existing post that well explains, How to filter specific apps for ACTION_SEND intent (and set a different text for each app)

Community
  • 1
  • 1
Swaroop
  • 532
  • 1
  • 4
  • 16
  • I don't want to specify particular apps. Instead I would like to display apps of particular type means all apps which may support pdf or all apps which support doc and likewise. – r.bhardwaj May 28 '14 at 12:44