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

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
}

Related

How to get Image Name or Id from docx file using Open xml in C#?

I am using Mvc.Net API with open XML. I need to replace multiple images in .docx file.I replace the images in current scenario but I don't get any Id or Name of the Image at my code side so facing difficulties to replace those images.
Here is my code
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
string Id=doc.MainDocumentPart.GetIdOfPart(imgPart);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
Can I get the name of Image in ImagePart?
I would do something like this:
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
var imageId = document.MainDocumentPart.GetIdOfPart(imgPart.Id);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
Also
This answer could help you

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.

How to compress the files in Blackberry?

In my application I used html template and images for browser field and saved in the sdcard . Now I want to compress that html,image files and send to the PHP server. How can I compress that files and send to server? Provide me some samples that may help lot.
i tried this way... my code is
EDIT:
private void zipthefile() {
String out_path = "file:///SDCard/" + "newtemplate.zip";
String in_path = "file:///SDCard/" + "newtemplate.html";
InputStream inputStream = null;
GZIPOutputStream os = null;
try {
FileConnection fileConnection = (FileConnection) Connector
.open(in_path);//read the file from path
if (fileConnection.exists()) {
inputStream = fileConnection.openInputStream();
}
byte[] buffer = new byte[1024];
FileConnection path = (FileConnection) Connector
.open(out_path,
Connector.READ_WRITE);//create the out put file path
if (!path.exists()) {
path.create();
}
os = new GZIPOutputStream(path.openOutputStream());// for create the gzip file
int c;
while ((c = inputStream.read()) != -1) {
os.write(c);
}
} catch (Exception e) {
Dialog.alert("" + e.toString());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
}
}
this code working fine for single file but i want to compress all the file(more the one file)in the folder .
In case you are not familiar with them, I can tell you that in Java the stream classes follow the Decorator Pattern. These are meant to be piped to other streams to perform additional tasks. For instance, a FileOutputStream allows you to write bytes to a file, if you decorate it with a BufferedOutputStream then you get also buffering (big chunks of data are stored in RAM before being finally written to disc). Or if you decorate it with a GZIPOutputStream then you get also compression.
Example:
//To read compressed file:
InputStream is = new GZIPInputStream(new FileInputStream("full_compressed_file_path_here"));
//To write to a compressed file:
OutputStream os = new GZIPOutputStream(new FileOutputStream("full_compressed_file_path_here"));
This is a good tutorial covering basic I/O . Despite being written for JavaSE, you'll find it useful since most things work the same in BlackBerry.
In the API you have these classes available:
GZIPInputStream
GZIPOutputStream
ZLibInputStream
ZLibOutputStream
If you need to convert between streams and byte array use IOUtilities class or ByteArrayOutputStream and ByteArrayInputStream.

how to display images present in blackberry device into my application

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

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