-1

Hi Does anyone know who to display documents from a file? i am currently using DownloadManager to download a file and the firing an intent to open the file in other apps. but i'm looking for a way to view the document within my app rather than send it to another?

heres what i'm currently doing

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                            request.setTitle(filename);
                            // in order for this if to run, you must use the android 3.2 to compile your app
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                request.allowScanningByMediaScanner();
                                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                            }

                            ContextWrapper c = new ContextWrapper(getBaseContext());
                            final String filePath = c.getFilesDir().getPath();
                            request.setDestinationInExternalFilesDir(getBaseContext(), filePath, filename);

                            // get download service and enqueue file
                            DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                            manager.enqueue(request);
                            Toast.makeText(getBaseContext(), "Downloading...", Toast.LENGTH_LONG).show();

                            BroadcastReceiver onComplete = new BroadcastReceiver() {

                                private String fileExt(String url){
                                    if (url.indexOf("?")>-1) {
                                        url = url.substring(0,url.indexOf("?"));
                                    }
                                    if (url.lastIndexOf(".") == -1) {
                                        return null;
                                    } else {
                                        String ext = url.substring(url.lastIndexOf(".") );
                                        if (ext.indexOf("%")>-1) {
                                            ext = ext.substring(0,ext.indexOf("%"));
                                        }
                                        if (ext.indexOf("/")>-1) {
                                            ext = ext.substring(0,ext.indexOf("/"));
                                        }
                                        return ext.toLowerCase();

                                    }
                                }

                                @Override
                                public void onReceive(Context context, Intent intent) {



                                    String path = getFilesDir().getPath() +"/" + filename;
                                    File f = new File(path); 



                                    MimeTypeMap myMime = MimeTypeMap.getSingleton();

                                    Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);

                                    //Intent newIntent = new Intent(Intent.ACTION_VIEW);
                                    String mimeType = myMime.getMimeTypeFromExtension(fileExt(f.toString()).substring(1));
                                    newIntent.setDataAndType(Uri.fromFile(f), mimeType);
                                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    try {
                                        context.startActivity(newIntent);
                                    } catch (android.content.ActivityNotFoundException e) {
                                        Toast.makeText(context, "No handler for this type of file.", 4000).show();
                                    }
                                }


                            };

                            registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Luke Batley
  • 2,384
  • 10
  • 44
  • 76
  • 1
    Different document types requires different viewer code (e.g. text file vs. pdf vs. spreadsheet). What have you tried to implement or at least scope out as the kind of document you want to show. – Morrison Chang Jan 21 '14 at 22:22
  • you can add some action that match the action you open in your activity and in the filtration action will choose your activity to work with it – mohammed momn Jan 21 '14 at 22:26
  • its a range of document types so doc dock pdf xl xls – Luke Batley Jan 21 '14 at 22:33

2 Answers2

1

Each of the usual office-ish type documents requires its own non-trivial viewer. Some may have readily available open source implementations but might not have the performance which would give a good user experience.

I would still recommend using intents to direct viewing but you may though want to drive your users to QuickOffice which Google has available for free in the Play store should they not have an existing viewer.

From the Android documentation:

http://developer.android.com/guide/components/intents-filters.html

Intent intent = new Intent(Intent.ACTION_SEND);
...

// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);

// Verify the intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent); 
}
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
0

If you want to view a file within your app then you are going to need to either implement your own viewer from scratch or find an open source implementation you can leverage. Keep in mind that if the document is of an arbitrary type you are going to need viewers for each file type you want to display. Here's a thread about creating such a viewer for PDF's/

This may not be useful to you but I should also mention that you can invoke an external app to appear as though it is a part of your app and not have its own entry within the list of open apps.

AFAIK these are the only options available.

Community
  • 1
  • 1
Nick
  • 8,181
  • 4
  • 38
  • 63