简体   繁体   English

如何通过Android 5中的Intent将文件发送到Google云打印应用程序?

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

I'm trying to tell the google cloud printing app to print a document, be it a .png file or an pdf. 我正在尝试告诉Google云打印应用程序打印文档,无论是.png文件还是pdf。 After checking if the app is installed via 在检查应用程序是否通过安装

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

i send the user to the PrintingDialogActivity if it is missing, as described here: https://developers.google.com/cloud-print/docs/android 如果缺少用户,我会将用户发送到PrintingDialogActivity,如此处所述: https : //developers.google.com/cloud-print/docs/android

But i would like to use the app, if it is installed. 但我想使用该应用程序(如果已安装)。 If i send the following intent: 如果我发送以下意图:

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

The getMimeType method is this: 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;

} }

it returns "application/pdf" for pdf files. 对于pdf文件,它将返回“ application / pdf”。

On my android 4.0.3 device i get the action chooser and can choose the google cloud print app, which then allows to do stuff like saving the file to google drive. 在我的android 4.0.3设备上,我得到了动作选择器,可以选择google cloud print应用程序,然后可以执行诸如将文件保存到google drive的操作。 It works. 有用。

But if i start this intent on my Android 5.1.1 device (Nexus 5), the action chooser also opens, but it doesn't have the google cloud printing app or anything else printing related in it. 但是,如果我在Android 5.1.1设备(Nexus 5)上启动此意图,则操作选择器也会打开,但其中没有google cloud打印应用程序或任何其他与打印相关的应用程序。 Cloud print is preinstalled and currently on version 1.17b. 云打印已预安装,当前版本为1.17b。 I didn't kill it's process with some form of energy saving app. 我并没有使用某种形式的节能应用程序杀死它的过程。 The device is not routed. 设备未路由。 What am i missing? 我想念什么?

I also tried entering "text/html" by hand as the mime type, because that was the solution to another stackoverflow thread - but it doesn't solve mine. 我还尝试过手动输入“ text / html”作为mime类型,因为这是另一个stackoverflow线程的解决方案-但这不能解决我的问题。

Setting the mimetype to */* also doesn't make the actionchooser offer me the printer 将mimetype设置为* / *也不会使actionchooser为我提供打印机

After a lot of googling and testing it is rather unclear to me, if printing via intent is still possible on android 5 with google cloud print. 经过大量的谷歌搜索和测试后,对我来说还很不清楚,如果在使用谷歌云打印的Android 5上仍然可以通过意图进行打印。 BUT what does work is to create a custom PrintDocumentAdapter, as described in this post: https://stackoverflow.com/a/20719729/1171328 但是有效的方法是创建一个自定义PrintDocumentAdapter,如这篇文章中所述: https : //stackoverflow.com/a/20719729/1171328

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM