我试图告诉谷歌云打印应用程序打印文档,无论是 .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 设备上,我得到了动作选择器,并且可以选择谷歌云打印应用程序,然后允许执行诸如将文件保存到谷歌驱动器之类的操作。有用。
但是,如果我在我的 Android 5.1.1 设备(Nexus 5)上启动此意图,动作选择器也会打开,但它没有谷歌云打印应用程序或其他任何与打印相关的内容。云打印已预安装,当前版本为 1.17b。我没有使用某种形式的节能应用程序来终止它的进程。设备未路由。我错过了什么?
我还尝试手动输入“text/html”作为 mime 类型,因为这是另一个 stackoverflow 线程的解决方案 - 但它不能解决我的问题。
将 mimetype 设置为 */* 也不会让 actionchooser 为我提供打印机