Open pdf, doc, ppt or xls on a BlackBerry - blackberry

How do I open pdf, doc, ppt or xls programmatically on a BlackBerry?

The best approach is to purchase a 3rd-party viewer and launch that to view the document:
String path = "file://SDCard/info.doc";
String contentType = MIMETypeAssociations.getMIMEType(path);
Invocation invocation = new Invocation(path, contentType, null, false, ContentHandler.ACTION_OPEN);
Registry registry = Registry.getRegistry(MyApp.class.getName());
try {
ContentHandler[] handlers = registry.findHandler(invocation);
if (handlers != null && handlers.length > 0) {
invocation.setID(handlers[0].getID());
registry.invoke(invocation);
} else {
Dialog.alert("Error");
}
} catch (Exception e) {
Dialog.alert("Error");
}

Related

JavaMail MIME attachment link by cid

Background
I have banged my head against this for a while and not made much progress. I am generating MPEG_4 / AAC files in Android and sending them by email as .mp3 files. I know they aren't actually .mp3 files, but that allows Hotmail and Gmail to play them in Preview. They don't work on iPhone though, unless they are sent as .m4a files instead which breaks the Outlook / Gmail Preview.
So I have thought of a different approach which is to attach as a .mp3 file but have an HTML link in the email body which allows the attached file to be downloaded and specifies a .m4a file name. Gmail / Outlook users can click the attachment directly whereas iPhone users can use the HTML link.
Issue
I can send an email using JavaMail with HTML in it including a link which should be pointing at the attached file to allow download of that file by the link. Clicking on the link in Gmail (Chrome on PC) gives a 404 page and iPhone just ignores my clicking on the link.
Below is the code in which I generate a multipart message and assign a CID to the attachment which I then try to access using the link in the html part. It feels like I am close, but maybe that is an illusion. I'd be massively grateful if someone could help me fix it or save me the pain if it isn't possible.
private int send_email_temp(){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtp_host_setting);
//props.put("mail.debug", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", smtp_port_setting);
session = Session.getInstance(props);
ActuallySendAsync_temp asy = new ActuallySendAsync_temp(true);
asy.execute();
return 0;
}
class ActuallySendAsync_temp extends AsyncTask<String, String, Void> {
public ActuallySendAsync_temp(boolean boo) {
// something to do before sending email
}
#Override
protected Void doInBackground(String... params) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient_email_address));
message.setSubject(email_subject);
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
String file = mFileName;
/**/
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
/* /
File ff = new File(file);
try {
messageBodyPart.attachFile(ff);
} catch(IOException eio) {
Log.e("Message Error", "Old Macdonald");
}
/* /
messageBodyPart = new PreencodedMimeBodyPart("base64");
byte[] file_bytes = null;
File ff = new File(file);
try {
int length = (int) ff.length();
BufferedInputStream reader = new BufferedInputStream(new FileInputStream(ff));
file_bytes = new byte[length];
reader.read(file_bytes, 0, length);
reader.close();
} catch (IOException eio) {
Log.e("Message Error", "Old Macdonald");
}
messageBodyPart.setText(Base64.encodeToString(file_bytes, Base64.DEFAULT));
messageBodyPart.setHeader("Content-Transfer-Encoding", "base64");
/**/
messageBodyPart.setFileName( DEFAULT_AUDIO_FILENAME );//"AudioClip.mp3");
//messageBodyPart.setContentID("<audio_clip>");
String content_id = UUID.randomUUID().toString();
messageBodyPart.setContentID("<" + content_id + ">");
messageBodyPart.setDisposition(Part.ATTACHMENT);//INLINE);
messageBodyPart.setHeader("Content-Type", "audio/mp4");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart messageBodyText = new MimeBodyPart();
//final String MY_HTML_MESSAGE = "<h1>My HTML</h1><a download=\"AudioClip.m4a\" href=\"cid:audio_clip\">iPhone Download</a>";
final String MY_HTML_MESSAGE = "<h1>My HTML</h1><a download=\"AudioClip.m4a\" href=\"cid:" + content_id + "\">iPhone Download</a>";
messageBodyText.setContent( MY_HTML_MESSAGE, "text/html");
multipart.addBodyPart(messageBodyText);
message.setContent(multipart);
Print_Message_To_Console(message);
Transport transport = session.getTransport("smtp");
transport.connect(smtp_host_setting, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// something to do after sending email
}
}
int Print_Message_To_Console(Message msg) {
int ret_val = 0;
int line_num = 0;
InputStream in = null;
InputStreamReader inputStreamReader = null;
BufferedReader buff_reader = null;
try {
in = msg.getInputStream();
inputStreamReader = new InputStreamReader(in);
buff_reader = new BufferedReader(inputStreamReader);
String temp = "";
while ((temp = buff_reader.readLine()) != null) {
Log.d("Message Line " + Integer.toString(line_num++), temp);
}
} catch(Exception e) {
Log.d("Message Lines", "------------ OOPS! ------------");
ret_val = 1;
} finally {
try {
if (buff_reader != null) buff_reader.close();
if (inputStreamReader != null) inputStreamReader.close();
if (in != null) in.close();
} catch(Exception e2) {
Log.d("Message Lines", "----------- OOPS! 2 -----------");
ret_val = 2;
}
}
return ret_val;
}
You need to create a multipart/related and set the main text part as the first body part.

Xamarin - IOS - Video assets in Photo library - Find By name

I'm new in Xamarin development.
I build my app, where user can clicks on DOWNLOAD button.
This button download video from the server and save to Photo library.
Here is how I implement this (maybe its incorrect way??)
public bool SaveVideo(byte[] videoData, int id)
{
try
{
CreateCustomAlbum();
// Save file to applicaiton folder
string local_path = SaveFileToApplicationFolder(videoData);
_lib.WriteVideoToSavedPhotosAlbum(new Foundation.NSUrl(local_path), (t, u) =>
{
DeleteLocalFile(local_path); // HERE I DELETE FILE FOR NOT INCREASE SIZE OF APPLICATION
_local_file_path = t.AbsoluteUrl.ToString(); // global variable
_lib.Enumerate(ALAssetsGroupType.Album, HandleALAssetsLibraryGroupsEnumerationResultsDelegate, (obj) => { });
});
return true;
}
catch (Exception ex)
{
return false;
}
}
void DeleteLocalFile(string local_path)
{
try
{
if (File.Exists(local_path))
{
File.Delete(local_path);
if (!File.Exists(local_path))
{
Console.WriteLine("Deleted");
}
}
}
catch (Exception ex)
{
}
}
string SaveFileToApplicationFolder(byte[] videoData)
{
try
{
string file_path = String.Empty;
var doc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filename = "MY-APP-" + id + ".mp4"; // id global variable
file_path = Path.Combine(doc, filename); // global variable
File.WriteAllBytes(file_path, videoData);
return file_path;
}
catch (Exception ex)
{
return String.Empty;
}
}
void HandleALAssetsLibraryGroupsEnumerationResultsDelegate(ALAssetsGroup group, ref bool stop)
{
try
{
if (group == null)
{
stop = true;
return;
}
if (group.Name == "MY-APP-ALBUM-NAME")
{
stop = true;
_current_album = group;
SaveFileToCustomAlbum();
}
}
catch (Exception ex)
{
}
}
void SaveFileToCustomAlbum()
{
try
{
if (_current_album != null && !String.IsNullOrEmpty(_local_file_path))
{
_lib.AssetForUrl(new Foundation.NSUrl(_local_file_path), delegate (ALAsset asset)
{
if (asset != null)
{
_current_album.AddAsset(asset);
}
else
{
Console.WriteLine("ASSET == NULL.");
}
}, delegate (NSError assetError)
{
Console.WriteLine(assetError.ToString());
});
}
}
catch (Exception ex)
{
}
}
So this code do:
1) Save video to local folder my video - Method SaveFileToApplicationFolder
2) Then Save video file to Photo library - Method SaveVideo
3) Then Delete file from app folder (in purpose not increase application folder size (app size) --- IF ITS CORRECT logic??
4) Then put assets to Custom Album for my App
SO everything here works well for me......BUT!
I need overtime when user open item - check if he already has video for this item in photos library or not?
And here I'm stack....i just don't understand how i can to check if user has specific video?? I don't find hot to set NAME for ASSETS and hot looking for assets by name...so don't know hot to find this assets....METADATA?? Key_VALUE of object??
Refer to Obj-C Check if image already exists in Photos gallery
In short:
Store assetUrl when saving video with NSUserDefaults
Check if the video exists in Photo library with assetUrl when next time to open it .
You may just want to use xam.plugin.media nuget package. It makes it very easy to take and store videos as well as access the default video picker for selecting existing videos

How to force excel file to open in browser instead of download?

I am working with MVC5 Crystal reports of pdf and excel files, my code works well for pdf to view in the web browser but when i change content-type to excel file its download only,
public ActionResult Summary(string startDate, string endDate, string summaryBy, string reportType)
{
using (MMTModel entity = new MMTModel())
{
string CryRpt_Name = null;
ObjectResult<DeeqtoonSummary> ObjRsl = null;
if (reportType == "Summary")
{
CryRpt_Name = "Summary.rpt";
ObjRsl = entity.rpt_Summary(startDate, endDate, summaryBy);
}
else if (reportType == "Detail")
{
CryRpt_Name = "Detail.rpt";
ObjRsl = entity.rpt_Detail(startDate, endDate);
}
ReportDocument rpt = new ReportDocument();
rpt.Load(Path.Combine(rpt.FileName = Server.MapPath("~/CrystalReports"), CryRpt_Name));
List<Summary> ObjRslLst = ObjRsl.ToList();
rpt.SetDataSource(ObjRslLst);
try
{
//Excel file
Stream stream = rpt.ExportToStream(ExportFormatType.Excel);
return File(stream, "application/vnd.ms-excel");
//pdf file
Stream stream = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
return File(stream, "application/pdf");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
How can i force to open the excel file in the web browser?
I don't know anything about crystal-reports, but if your browser doesn't have an implementation for viewing excel documents then it will just download them. I have never been able to open an excel document with my browser unless the web application itself implements the display of the document.
Basically, you have to implement the MVC that displays an excel document yourself. Your browser doesn't support it out of the box.

Downloading Docusign PDF in Grails, file corrupted

Using Groovy 1.8.6 and Grails 2.1.0
Using embedded API, after user signs document, browser is redirected back to my app. Using "Get Envelope Documents and Certificate" API to download document to server. URL format:
"${baseUrl}/envelopes/${envelopeId}/documents/combined"
Code snippet (with minor details removed):
private void getDocument(requestUrl) {
def connection = urlConnect(requestUrl, null, "GET")
if (connection.responseCode == 200) {
savePDF(envelopeId, connection.inputStream)
}
}
private void savePDF(envelopeId, inputStream) {
String filePath = getSavedPDFPath(envelopeId)
def pdfWriter = new File(filePath).newWriter()
pdfWriter << inputStream
pdfWriter.close()
}
What happens is that the resulting file is not 100% correct, Adobe Reader complains that "at least one signature is invalid". Reader at least knows that the file was signed by DocuSign, Inc., and can show details about the certificate.
Per the Question's comment thread, the issue was being caused by the way the file was being saved. Using this code instead, the file saves / opens correctly:
private void savePDF(envelopeId, connection)
{
FileOutputStream fop = null;
File file;
String filePath = getSavedPDFPath(envelopeId);
try {
file = new File(filePath);
fop = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int numRead;
while((numRead = connection.getInputStream().read(buffer)) > 0)
{
fop.write(buffer, 0, numRead);
}
fop.flush();
fop.close();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

Mobile web application: download attachments on iOS

In my mobile web application i have one page within user can view attachments.
The attachment can be any type of file (jpg,png,txt,doc,zip, etc).
The view attachment action is in the form of <a> tag that points to an aspx file that process the request.
HTML:
<a class="attachBtn" href="_layouts/ViewFile.aspx?messageAttachmentInstanceId={some id}"></a>
ViewFile.aspx:
public partial class ViewFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.IO.BinaryWriter bw = null;
System.IO.MemoryStream ms = null;
System.IO.StreamReader sr = null;
try
{
string contentType = string.Empty;
byte[] content = null;
string fileName = string.Empty;
if (!string.IsNullOrEmpty(Request.QueryString["messageAttachmentInstanceId"]) &&
!string.IsNullOrEmpty(Request.QueryString["messageInstanceId"]))
{
int messageInstanceId = Int32.Parse(Request.QueryString["messageInstanceId"]);
Guid attachmentInstanceId;
GuidUtil.TryParse(Request.QueryString["messageAttachmentInstanceId"], out attachmentInstanceId);
MessageInstance messageInstance = WorkflowEngineHttpModule.Engine.GetService<IMessagingService>()
.GetMessageInstance(messageInstanceId);
if (messageInstance != null)
{
MessageAttachmentInstance attachmentInstnace = messageInstance.Attachments[attachmentInstanceId];
contentType = attachmentInstnace.ContentType;
fileName = attachmentInstnace.FileName;
content = attachmentInstnace.Content;
}
}
this.Response.ContentType = contentType;
string headerValue = string.Format("attachment;filename={0}",
this.Server.UrlPathEncode(fileName));
Response.AddHeader("content-disposition", headerValue);
bw = new System.IO.BinaryWriter(this.Response.OutputStream);
bw.Write(content);
}
catch (Exception ex)
{
LogError("ViewFile.aspx, "
+ ex.InnerException, ex);
}
finally
{
if (sr != null)
sr.Close();
if (ms != null)
ms.Close();
if (bw != null)
bw.Close();
}
}
}
The Problem:
in Android devices when user click on attachment the file is downloaded automatically which is the desirable behavior because the user can open the file later with any tool he wants and even if the file type is not supported user can later on download a tool which can open it.
but in iOS devices the file is not downloaded but instead redirects to ViewFile.aspx and tries to open the file within the browser and if the file type is not supported it shows alert: "safari cannot download this file".
even if the file type is supported i want it to be downloaded and not open by default.
How can i achieve this behavior?
AFAIK, you cannot download files on iOS.
Known files that Safari (or any app that has registered a file type, e.g. ZIP) supports will open or show a dialog letting the user choose how to open the file.
You can't control the behavior from your web app/site.

Resources