[英]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.