2

Hi i am downloading a file from URL to my files directory using download manager and then trying to open it with Action_View intent. When i run the Action_View it lets me select quick office and but says can't open file. but when i click on the file from the notification bar it opens fine. Does anyone know how to correctly open a file from an app? what i'm trying to achieve is download a file from url detect when it has finished downloading and allow the user to choose which app to open it in.

heres what i have tried so far

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() + "/";
                    Log.v("Search", "filePath = " + filePath);
                    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() {

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

                            openFile(filename);
                        }

                        protected void openFile(String fileName) {
                            String path = getFilesDir().getPath() +"/" + fileName;
                            File f = new File(path); 

                            String Extension = Global.getFileExt(fileName);

                            MimeTypeMap myMime = MimeTypeMap.getSingleton();
                            String mimeType = myMime.getMimeTypeFromExtension(Extension);

                            try {

                                Intent intent = new Intent();
                                intent.setAction(android.content.Intent.ACTION_VIEW);
                                File file = new File(path);
                                intent.setDataAndType(Uri.fromFile(file),mimeType);
                                startActivity(intent);  


                            } catch (android.content.ActivityNotFoundException e) {
                                Toast.makeText(getBaseContext(), "No handler for this type of file.", 4000).show();
                            }
                        }


                    };

                    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

                } else{

                }

                return false;
            }else{
                view.loadUrl(url);
                return true;
            }
        }
Luke Batley
  • 2,384
  • 10
  • 44
  • 76
  • 1
    Take a look at my answer to this question, I think it's pretty much the same issue: http://stackoverflow.com/questions/21304489/how-to-open-private-files-saved-to-the-internal-storage-using-intent-action-view/21305564#21305564 – NigelK Jan 29 '14 at 21:15

0 Answers0