从我的apps文件目录android中的另一个应用程序中打开文件

时间:2014-01-29 21:09:00

标签: android android-intent broadcastreceiver

您好我正在使用下载管理器将文件从URL下载到我的文件目录,然后尝试使用Action_View意图打开它。当我运行Action_View时,它允许我选择快速办公室,但说无法打开文件。但是当我点击通知栏中的文件时,它打开正常。有谁知道如何从应用程序正确打开文件?我想要实现的是在完成下载后从url detect下载文件,并允许用户选择打开它的应用程序。

继承人到目前为止我所尝试过的事情

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

0 个答案:

没有答案