简体   繁体   中英

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. 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

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:

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.

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. 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. Cloud print is preinstalled and currently on version 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.

Setting the mimetype to */* also doesn't make the actionchooser offer me the printer

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. BUT what does work is to create a custom PrintDocumentAdapter, as described in this post: https://stackoverflow.com/a/20719729/1171328

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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