how to display images present in blackberry device into my application - blackberry

I am developing a blackberry application where i want to select an image present in device and display it in my application. How to do it.
UPDATE
hi I used FilePicker to get the path of the file and i am storing it in "Selection(String)"
and i am using below code to display image in my application but i am getting exception. can anybody tell me where i did mistake.
try {
FileConnection fconn = (FileConnection)Connector.open(selection,Connector.READ);
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (fconn.exists()) {
InputStream input = fconn.openInputStream();
int available = input.available();
byte[] data = new byte[available];
input.read(data, 0, available);
EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);
Bitmap b = image.getBitmap();
BitmapField picture = new BitmapField(b);
add(picture);
add(new LabelField("Data Length:" + data.length));
}
else {
add(new LabelField("Picture does not exist"));
}
fconn.close();
}
catch (Exception ioe) {
add(new LabelField("Error"));
}

If your target OS is 6.0+ you can use RIM component FilePicker.
For lower OS versions you can use also this component: File Selection Popup

Related

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.

My App is not loaded in Blackberry simulator

I am using following code to create xml file -
void createxml(){
Document d = new Document();
Element root = d.createElement("","company");
Element employee = d.createElement("","employee");
employee.setAttribute("","id","1");
Element fname = d.createElement("","fname");
fname.addChild(Node.TEXT,"Vasudev");
Element lname = d.createElement("","lname");
lname.addChild(Node.TEXT,"Kamath");
Element address = d.createElement(Node.TEXT+"","address");
address.addChild(Node.TEXT,"Karkala");
employee.addChild(Node.ELEMENT,fname);
employee.addChild(Node.ELEMENT,lname);
employee.addChild(Node.ELEMENT,address);
root.addChild(Node.ELEMENT,employee);
d.addChild(Node.ELEMENT,root);
String fileName = "file:///SDCard/Blackberry/company.xml";
DataOutputStream os = null;
FileConnection fc = null;
try
{
fc = (FileConnection)Connector.open(fileName,Connector.READ_WRITE);
if (! fc.exists())
fc.create();
os = fc.openDataOutputStream();
KXmlSerializer serializer = new KXmlSerializer();
serializer.setOutput(os, "UTF-8");
d.write(serializer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But when I write this method, my program is not going to be load on the simulator, if I comment It loads easily. How do i solve this problem?
Do u simulate your app?
If you running in simulator, create a filder SDCard in your system and then create a sub folder Blackberry. And when you run your app take take the 'Simulate' menu > Change Sd card> Add Directory> and browse for the folder SDcard .... then run you app

BlackBerry logger

I am writing a text file on the BlackBerry simulator using the following code. My data is written successfully to the file but I am not able to find my file. Where can I can find my file on the simulator?
public static void debug() {
OutputStream os = null;
FileConnection fc = null;
try {
String data = "Hello This is Yogesh working on blackberry";
Dialog.alert("TEST_1");
fc = (FileConnection)Connector.open("file:///SDCard/test.txt",Connector.READ_WRITE);
//fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/test.txt",Connector.READ_WRITE);
Dialog.alert("TEST_2");
//FileConnection fc = (FileConnection)Connector.open(System.getProperty("file:///SDCard/"+"test.txt"),Connector.READ_WRITE);
Dialog.alert("TEST_3");
if(!fc.exists()) {
Dialog.alert("TEST_31");
fc.create();
}
Dialog.alert("TEST_4");
os = fc.openOutputStream();
Dialog.alert("TEST_5");
os.write(data.getBytes());
Dialog.alert("TEST_6");
Dialog.alert("Data written successfully...!!!");
} catch(Exception e) {
Dialog.alert("Exception in writing logs :"+e);
} finally {
if(os != null) {
try {
os.close();
} catch(Exception e) { }
}
if(fc != null) {
try {
fc.close();
} catch(Exception e) { }
}
}
Within the simulator, if you open the Files application, in the 'Applications' folder, you can navigate through the SDCard files, by selecting File Folders, and then Media Card.
From within the simulator you can also go to Media, click the menu button, select Explore and browse to your file.
Another option is to set your simulator to save the content to a chosen location on the file system: in Eclipse go to Run -> Debug Configurations -> Simulator tab -> Memory tab. Check the "Use PC file system for SD Card files" option and browse to your preferred location. I found this to be very convenient.
Hope this helps :)

How to retrieve data from a attached zip file in Blackberry application?

I am using eclipse to build application for Blackberry. I attached a zip file with my application. Please help me, I don't know how to retrieve data form the zip file in application development.
In BlackBerry we can use two compression standarts: GZip and ZLib.
Choose one, then compress your file and add to project.
Then you should be able to open it as an resource.
After that decompress it with GZIPInputStream or ZLibInputStream accordingly.
Example (uncompress and print text from test.gz attached to project):
try
{
InputStream inputStream = getClass().getResourceAsStream("test.gz");
GZIPInputStream gzis = new GZIPInputStream(inputStream);
StringBuffer sb = new StringBuffer();
int i;
while ((i = gzis.read()) != -1)
{
sb.append((char)i);
}
String data = sb.toString();
add(new RichTextField(data));
gzis.close();
}
catch(IOException ioe)
{
//do something here
}

Accessing the 'Media' directory of a Blackberry within the JDK

Trying to use JSR 75 to access media saved under the '/home/video/' directory on the device. Using Blackbery JDK 4.6.1. Single line of code throws a 'FileSystem IO Error' Exception. Which is, as usual, unhelpful in the extreme.
fconn = (FileConnection)Connector.open("file:///home/user/videos/"+name, Connector.READ);
Has anyone tried to do this? I can open files within my jar, but can't seem to access the media folder. I have the javax.microedition.io.Connector.file.read permission set and my appplication is signed.
There are two kind of filesystems on BlackBerry - SDCard and store. You have to use one of them, defining it in the path. Standard directory on SDCard where video, music etc stored is "file:///SDCard/BlackBerry".
String standardPath = "file:///SDCard/BlackBerry";
String videoDir = System.getProperty("fileconn.dir.videos.name");
String fileName = "video.txt";
String path = standardPath+"/"+videoDir+"/"+fileName;
String content = "";
FileConnection fconn = null;
DataInputStream is = null;
ByteVector bytes = new ByteVector();
try {
fconn = (FileConnection) Connector.open(path, Connector.READ);
is = fconn.openDataInputStream();
int c = is.read();
while(-1 != c)
{
bytes.addElement((byte) (c));
c = is.read();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
content = new String(bytes.toArray());
add(new RichTextField(content));
See also
SUN Dev Network - Getting Started with the FileConnection APIs
RIM Forum - Some questions about FileConnection/JSR 75
Use System.getProperty("fileconn.dir.memorycard") to check if SDCard available
How to save & delete a Bitmap image in Blackberry Storm?

Resources