This is an update to my btPrint4 Android application. The app now supports ‘printing’ of files. You are no longer tied to the provided demo files. ‘Printing’ here means it sends the file as is to the printer. So, watch your step and do not send files that your printer does not understand.
Added a file browser activity: a class named item to hold file information, a FileChooser
class and a FileArrayAdapter
. Then the needed layout files are added.
The main screen now has a new button to start the FileBrowser
(FileChooser
).
boolean bFileBrowserStarted=false;
void startFileBrowser(){
if(bFileBrowserStarted)
return;
bFileBrowserStarted=true;
Intent intent1 = new Intent(this, FileChooser.class);
startActivityForResult(intent1, REQUEST_SELECT_FILE);
}
The main code is extended to recognize the new activity result:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (D) Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_SELECT_FILE:
if (resultCode == RESULT_OK) {
String curFileName = data.getStringExtra("GetFileName");
String curPath = data.getStringExtra("GetPath");
if(!curPath.endsWith("/")) {
curPath += "/";
}
String fullName=curPath+curFileName;
mTxtFilename.setText(fullName);
addLog("Filebrowser Result_OK: '"+fullName+"'");
}
bFileBrowserStarted=false;
break;
case REQUEST_SELECT_DEMO_FILE:
The code to print was simple to change, as we just need to distinguish between an asset and a real file:
void printFile() {
String fileName = mTxtFilename.getText().toString();
if (btPrintService.getState() != btPrintFile.STATE_CONNECTED) {
myToast("Please connect first!", "Error");
return; }
if (fileName.startsWith("escp")) {
byte[] bufQuery = escpQuery();
btPrintService.write(bufQuery);
}
if (mTxtFilename.length() > 0) {
InputStream inputStream = null;
ByteArrayInputStream byteArrayInputStream;
Integer totalWrite = 0;
StringBuffer sb = new StringBuffer();
try {
if(fileName.startsWith("/")){
inputStream = new FileInputStream(fileName);
addLog("Using regular file: '"+fileName+"'");
}
else {
inputStream = this.getAssets().open(fileName);
addLog("Using demo file: '"+fileName+"'");
}
byte[] buf = new byte[2048];
int readCount = 0;
...
Code changes at GitHub
Download APK