2014-01-21 138 views
-1

嗨有誰知道誰顯示文件中的文件?我目前正在使用DownloadManager下載一個文件,併發射意圖在其他應用程序中打開該文件。但我正在尋找一種方法來查看我的應用程序內的文件,而不是將其發送到另一個?顯示來自文件的文件android

繼承人什麼,我目前正在做

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)); 
+1

不同的文檔類型需要不同的觀看者代碼(例如文本文件與PDF與電子表格)。你試圖實施什麼,或者至少是你希望展示的文檔的範圍。 –

+0

您可以添加一些與您在活動中打開的操作相匹配的操作,並且在過濾操作中將選擇您的活動以使用它。 –

+0

它的一系列文檔類型,以便doc dock pdf xl xls –

回答

1

每個常用的office-ish類型的文檔都需要自己的非平凡查看器。有些可能具有現成的開源實現,但可能沒有提供良好用戶體驗的性能。

我仍然會建議您使用意圖直接查看,但您可能仍希望將您的用戶驅動到Google Play在Play商店中免費提供的QuickOffice(如果它們沒有現有查看器)。

從Android文檔:

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

如果你想查看你的應用程序中的文件,那麼你將需要要麼從頭開始實現自己的瀏覽器或找到一個開源實現,你可以槓桿。請記住,如果文檔屬於任意類型,那麼您將需要查看器來查看每個要顯示的文件類型。 Here's a thread關於爲PDF創建此類查看器/

這可能對您沒有用處,但我還應該提及您可以調用外部應用程序,使其看起來好像它是您的應用程序的一部分,並且沒有其自己的條目打開的應用程序列表。

AFAIK這些是唯一可用的選項。

相關問題