public Object invoke(Object obj, Object[] args) throws Exception { // input = (email address, subject, body, full attachment path) String email = (String) args[0].toString(); String subject = (String) args[1].toString(); String body = (String) args[2].toString(); String attachment = (String) args[3].toString(); String fName = attachment; byte[] data = null; FileConnection fconn = null; DataInputStream is = null; try { fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE); is = fconn.openDataInputStream(); data = IOUtilities.streamToBytes(is); is.close(); fconn.close(); } catch (Exception ex) { Dialog.inform("Error in file path: " + ex.toString()); return new Boolean(false); } // create a multipart Multipart mp = new Multipart(); // create the file SupportedAttachmentPart sap = new SupportedAttachmentPart(mp, "application/x-example", attachment, data); TextBodyPart tbp = new TextBodyPart(mp, body); // add the file to the multipart mp.addBodyPart(tbp); mp.addBodyPart(sap); // create a message in the sent items folder Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT); Message message = new Message(folders[0]); Address toAdd = new Address(email, "my email"); Address toAdds[] = new Address[1]; toAdds[0] = toAdd; message.addRecipients(Message.RecipientType.TO, toAdds); message.setSubject(subject); message.setContent(mp); Session session = Session.getDefaultInstance(); Transport trans = session.getTransport(); // add recipients to the message and send boolean sent = false; try { trans.send(message); sent = true; } catch (Exception e) { Dialog.inform("Error while sending: " + e.toString()); } return new Boolean(sent); }
private static Bitmap getDefaultHeadImage() throws Exception { if (sm_defaultHeadImage == null) { if (sm_mainApp == null && UiApplication.getUiApplication() instanceof recvMain) { sm_mainApp = (recvMain) UiApplication.getUiApplication(); } byte[] bytes = IOUtilities.streamToBytes( sm_mainApp .getClass() .getResourceAsStream( fsm_largeHeadImage ? "/defaultHeadImage_l.png" : "/defaultHeadImage.png")); sm_defaultHeadImage = EncodedImage.createEncodedImage(bytes, 0, bytes.length).getBitmap(); sm_defaultHeadImageHashCode = bytes.length; } return sm_defaultHeadImage; }
/** * This method reads from a text file and returns its contents * * @param fName Name of the file * @return returns the contents of the file if the file is found; null otherwise */ public String readTextFile(String fName) { String result = null; FileConnection fconn = null; DataInputStream is = null; // read the file try { fconn = (FileConnection) Connector.open("file:///SDCard/BlackBerry/" + fName); is = fconn.openDataInputStream(); byte[] data = IOUtilities.streamToBytes(is); result = new String(data); is .close(); // http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_-_Close_connections.html?nodeid=1261294&vernum=0 fconn.close(); } catch (Exception e) { System.out.println(e.getMessage()); } return result; // return the file contents }
public void run() { if (URL != null && URL.length() > 0) { ConnectionFactory f = new ConnectionFactory(); ConnectionDescriptor descr = f.getConnection(URL); String targetURL; targetURL = descr.getUrl(); HttpConnection httpConnection = null; DataOutputStream httpDataOutput = null; InputStream httpInput = null; int rc; try { httpConnection = (HttpConnection) Connector.open(targetURL); rc = httpConnection.getResponseCode(); if (rc != HttpConnection.HTTP_OK) { throw new IOException("HTTP response code: " + rc); } httpInput = httpConnection.openInputStream(); InputStream inp = httpInput; byte[] b = IOUtilities.streamToBytes(inp); final EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length); final Bitmap bmp = hai.getBitmap(); System.out.println(bmp.toString()); if (target != null) { UiApplication.getUiApplication() .invokeLater( new Runnable() { public void run() { // TODO Auto-generated method stub if (isDetailEvent == true) { target.setBitmap( DisplayHelper.CreateScaledCopyKeepAspectRatio( bmp, (int) (Display.getWidth() * 0.7), (int) (Display.getHeight() * 0.3))); } else { target.setBitmap( DisplayHelper.CreateScaledCopyKeepAspectRatio( bmp, target.getBitmapWidth(), target.getBitmapHeight())); } } }); } if (localPath != null) { Utils.saveBitmap(localPath, bmp); } if (imageCache != null) { if (Utils.saveBitmap( ImageCacheModel.getImageCacheDirectory() + imageCache.getFileName(), bmp)) { // CacheUtils.getInstance().addImageCache(imageCache); System.out.println("image write success"); } } if (callback != null) callback.onImageDownloaded(true, bmp); } catch (Exception ex) { System.out.println("URL Bitmap Error........" + ex.getMessage()); } finally { try { if (httpInput != null) httpInput.close(); if (httpDataOutput != null) httpDataOutput.close(); if (httpConnection != null) httpConnection.close(); } catch (Exception e) { e.printStackTrace(); } } } }