package com.rcreations.testprint;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Picture;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
public class PrintUtils
{
// for logging
private static final String TAG = PrintUtils.class.getSimpleName();
// Send 2 Printer package name
private static final String PACKAGE_NAME = "com.rcreations.send2printer";
// intent action to trigger printing
public static final String PRINT_ACTION = "com.rcreations.send2printer.print";
// content provider for accessing images on local sdcard from within html content
// sample img src shoul be something like "content://s2p_localfile/sdcard/logo.gif"
public static final String LOCAL_SDCARD_CONTENT_PROVIDER_PREFIX = "content://s2p_localfile";
/**
* Returns true if "Send 2 Printer" is installed.
*/
public static boolean isSend2PrinterInstalled( Context context )
{
boolean output = false;
PackageManager pm = context.getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo( PACKAGE_NAME, 0 );
if( pi != null )
{
output = true;
}
} catch (PackageManager.NameNotFoundException e) {}
return output;
}
/**
* Launches the Android Market page for installing "Send 2 Printer"
* and calls "finish()" on the given activity.
*/
public static void launchMarketPageForSend2Printer( final Activity context )
{
AlertDialog dlg = new AlertDialog.Builder( context )
.setTitle("Install Send 2 Printer")
.setMessage("Before you can print to a network printer, you need to install Send 2 Printer from the Android Market.")
.setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick( DialogInterface dialog, int which )
{
// launch browser
Uri data = Uri.parse( "http://market.android.com/search?q=pname:" + PACKAGE_NAME );
Intent intent = new Intent( android.content.Intent.ACTION_VIEW, data );
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity( intent );
// exit
context.finish();
}
} )
.show();
}
/**
* Save the given picture (contains canvas draw commands) to a file for printing.
*/
public static File saveCanvasPictureToTempFile( Picture picture )
{
File tempFile = null;
// save to temporary file
File dir = getTempDir();
if( dir != null )
{
FileOutputStream fos = null;
try
{
File f = File.createTempFile( "picture", ".stream", dir );
fos = new FileOutputStream( f );
picture.writeToStream( fos );
tempFile = f;
}
catch( IOException e )
{
Log.e( TAG, "failed to save picture", e );
}
finally
{
close( fos );
}
}
return tempFile;
}
/**
* Sends the given picture file (returned from {@link #saveCanvasPictureToTempFile}) for printing.
*/
public static boolean queuePictureStreamForPrinting( Context context, File f )
{
// send to print activity
Uri uri = Uri.fromFile( f );
Intent i = new Intent( PRINT_ACTION );
i.setDataAndType( uri, "application/x-android-picture-stream" );
i.putExtra( "scaleFitToPage", true );
context.startActivity( i );
return true;
}
/**
* Save the given Bitmap to a file for printing.
* Note: Bitmap can be result of canvas draw commands.
*/
public static File saveBitmapToTempFile( Bitmap b, Bitmap.CompressFormat format )
throws IOException, UnknownFormatException
{
File tempFile = null;
// save to temporary file
File dir = getTempDir();
if( dir != null )
{
FileOutputStream fos = null;
try
{
String strExt = null;
switch( format )
{
case PNG:
strExt = ".pngx";
break;
case JPEG:
strExt = ".jpgx";
break;
default:
throw new UnknownFormatException( "unknown format: " + format );
}
File f = File.createTempFile( "bitmap", strExt, dir );
fos = new FileOutputStream( f );
b.compress( format, 100, fos );
tempFile = f;
}
finally
{
close( fos );
}
}
return tempFile;
}
/**
* Sends the given image file for printing.
*/
public static boolean queueBitmapForPrinting( Context context, File f, Bitmap.CompressFormat format )
throws UnknownFormatException
{
String strMimeType = null;
switch( format )
{
case PNG:
strMimeType = "image/png";
break;
case JPEG:
strMimeType = "image/jpeg";
break;
default:
throw new UnknownFormatException( "unknown format: " + format );
}
// send to print activity
Uri uri = Uri.fromFile( f );
Intent i = new Intent( PRINT_ACTION );
i.setDataAndType( uri, strMimeType );
i.putExtra( "scaleFitToPage", true );
i.putExtra( "deleteAfterPrint", true );
context.startActivity( i );
return true;
}
/**
* Sends the given text for printing.
*/
public static boolean queueTextForPrinting( Context context, String strContent )
{
// send to print activity
Intent i = new Intent( PRINT_ACTION );
i.setType( "text/plain" );
i.putExtra( Intent.EXTRA_TEXT, strContent );
context.startActivity( i );
return true;
}
/**
* Save the given text to a file for printing.
*/
public static File saveTextToTempFile( String text )
throws IOException
{
File tempFile = null;
// save to temporary file
File dir = getTempDir();
if( dir != null )
{
FileOutputStream fos = null;
try
{
File f = File.createTempFile( "text", ".txt", dir );
fos = new FileOutputStream( f );
fos.write( text.getBytes() );
tempFile = f;
}
finally
{
close( fos );
}
}
return tempFile;
}
/**
* Sends the given text file for printing.
*/
public static boolean queueTextFileForPrinting( Context context, File f )
{
// send to print activity
Uri uri = Uri.fromFile( f );
Intent i = new Intent( PRINT_ACTION );
i.setDataAndType( uri, "text/plain" );
i.putExtra( "deleteAfterPrint", true );
context.startActivity( i );
return true;
}
/**
* Sends the given html for printing.
*
* You can also reference a local image on your sdcard using the "content://s2p_localfile" provider.
* For example: <img src="content://s2p_localfile/sdcard/logo.gif">
*/
public static boolean queueHtmlForPrinting( Context context, String strContent )
{
// send to print activity
Intent i = new Intent( PRINT_ACTION );
i.setType( "text/html" );
i.putExtra( Intent.EXTRA_TEXT, strContent );
context.startActivity( i );
return true;
}
/**
* Sends the given html URL for printing.
*
* You can also reference a local file on your sdcard using the "content://s2p_localfile" provider.
* For example: "content://s2p_localfile/sdcard/test.html"
*/
没有合适的资源?快使用搜索试试~ 我知道了~
android 无线打印实现源代码 附带send2Printer
共37个文件
class:18个
xml:4个
java:4个
5星 · 超过95%的资源 需积分: 45 941 下载量 180 浏览量
2012-08-21
09:50:36
上传
评论 26
收藏 463KB RAR 举报
温馨提示
本资源包括android无线连接和打印的全套工程,下载后首先安装send2printer,配置好默认打印机,并且可以打印出测试页。然后通过该源代码编译执行安装在基于android系统的移动终端上即可驱动打印。希望和大家多多交流啊!
资源推荐
资源详情
资源评论
收起资源包目录
TestPrint&Send2Printer.rar (37个子文件)
TestPrint
bin
classes.dex 16KB
res
drawable
icon.png 3KB
TestPrint.apk 15KB
jarlist.cache 119B
classes
com
rcreations
testprint
MainActivity.class 7KB
BuildConfig.class 357B
MainActivity$7.class 843B
MainActivity$3.class 839B
MainActivity$2.class 849B
MainActivity$1.class 841B
R$layout.class 412B
R$id.class 640B
R$drawable.class 418B
PrintUtils$1.class 1KB
R$attr.class 358B
PrintUtils.class 8KB
R.class 573B
MainActivity$4.class 839B
MainActivity$5.class 842B
MainActivity$6.class 843B
R$string.class 445B
PrintUtils$UnknownFormatException.class 511B
resources.ap_ 6KB
AndroidManifest.xml 723B
res
drawable
icon.png 3KB
values
strings.xml 173B
layout
main.xml 2KB
Send2Printer v1.4.4.apk 401KB
assets
gen
com
rcreations
testprint
BuildConfig.java 166B
R.java 1KB
TestPrint.apk 13KB
src
com
rcreations
testprint
PrintUtils.java 10KB
MainActivity.java 8KB
.project 845B
.classpath 380B
project.properties 582B
AndroidManifest.xml 723B
共 37 条
- 1
kunlong0909
- 粉丝: 234
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页