簡體   English   中英

如何通過Android 5中的Intent將文件發送到Google雲打印應用程序?

[英]How to send file to google cloud printing app via intent in android 5?

我正在嘗試告訴Google雲打印應用程序打印文檔,無論是.png文件還是pdf。 在檢查應用程序是否通過安裝

public static boolean isCloudPrintInstalled(Context ctx){
    String packageName = "com.google.android.apps.cloudprint";
    android.content.pm.PackageManager mPm = ctx.getPackageManager(); 
    try{
        PackageInfo info = mPm.getPackageInfo(packageName, 0); 
        boolean installed = (info != null);
        return installed;
    }catch(NameNotFoundException e){
        return false;
    }
}

如果缺少用戶,我會將用戶發送到PrintingDialogActivity,如此處所述: https : //developers.google.com/cloud-print/docs/android

但我想使用該應用程序(如果已安裝)。 如果我發送以下意圖:

File theFile = new File(filePath); //file is NOT missing
Intent printIntent = new Intent(Intent.ACTION_SEND);
printIntent.setType(Helper.getMimeType(filePath));
printIntent.putExtra(Intent.EXTRA_TITLE,  titleOfFile);
printIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(theFile));
ctx.startActivity(printIntent);

getMimeType方法是這樣的:

public static String getMimeType(String url) {
  String method = "Helper.getMimeType";
  String type = null;
  String extension = MimeTypeMap.getFileExtensionFromUrl(url);

  //Fix for Bug: https://code.google.com/p/android/issues/detail?id=5510
  if(extension == null || extension.equals("")){
     extension = url.substring(url.lastIndexOf('.'));
  }

  type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
  //should i consider just not using the MimeTypeMap? -.-
  if(type == null || type.equals("")){
      if(extension.toLowerCase(Locale.getDefault()).equals(".txt") == true){
          type = "text/plain";
      }else{
         Log.e(method, "Unknown extension");
      }
  }

  return type;

}

對於pdf文件,它將返回“ application / pdf”。

在我的android 4.0.3設備上,我得到了動作選擇器,可以選擇google cloud print應用程序,然后可以執行諸如將文件保存到google drive的操作。 有用。

但是,如果我在Android 5.1.1設備(Nexus 5)上啟動此意圖,則操作選擇器也會打開,但其中沒有google cloud打印應用程序或任何其他與打印相關的應用程序。 雲打印已預安裝,當前版本為1.17b。 我並沒有使用某種形式的節能應用程序殺死它的過程。 設備未路由。 我想念什么?

我還嘗試過手動輸入“ text / html”作為mime類型,因為這是另一個stackoverflow線程的解決方案-但這不能解決我的問題。

將mimetype設置為* / *也不會使actionchooser為我提供打印機

經過大量的谷歌搜索和測試后,對我來說還很不清楚,如果在使用谷歌雲打印的Android 5上仍然可以通過意圖進行打印。 但是有效的方法是創建一個自定義PrintDocumentAdapter,如這篇文章中所述: https : //stackoverflow.com/a/20719729/1171328

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM