I' doing an API for Black Berry but now I have a problem, the file don't save text inserted , for example when I write on application running : Hello , I guess that "Hello" is saving in EditField pt_nombre,
name = new LabelField("Nombre");
pt_nombre = new FixedWidthEditField();
String a = pt_nombre.getText();
String nom = name.getText();
String fullPath = "file:///SDCard/xxx.txt";
try {
FileConnection fconn = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE);
if (fconn.exists()) {
fconn.delete();
}
fconn.create();
OutputStream os = fconn.openOutputStream();
os.write(nombre.getBytes());
os.write("\r\n".getBytes());
os.write(a.getBytes());
fconn.close();
} catch (IOException e) {
System.out.println("Oh noes!!1! " + e.toString());
}
When I check the xxx.txt only are write the Label, Nombre: but the written text in EditField isn't !! help please
Are you sure this code even compiles? I don't see where nombre is declared (even to the extend that name and pt_nombre are 'declared'); nor is there anywhere that you set a value into pt_nombre, so I don't know what you rexpect to see in the file.
you should check FixedWidthEditField getText function.
will it return the text that user input
Related
I don't write code often and my knowledge is still low-level. I need help for something that I can't figure out
public static void SearchScriptEnd()
{
int counter = 0;
string line;
Report.Log(ReportLevel.Info, "Read Log", "Starting to find: Test Suite Ended");
var text = "Test Suite Ended";
if (File.Exists(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]))
{
StreamReader file =
new StreamReader(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]);
}else{
StreamReader file =
new StreamReader(TestSuite.Current.Parameters["LogPath"]);
}
while ((line = file.ReadLine()) != null)
{
if (line.Contains(text))
{
Report.Log(ReportLevel.Info, "Read Log", "[Success] Script End String has been found");
Report.Log(ReportLevel.Info, "Read Log", string.Format("Line number: '{0}'", counter));
return;
}
counter++;
}
Report.Log(ReportLevel.Failure, "Read Log", "[Missing] Anvil Script End String NOT found");
file.Close();
}
At first, the while was in both statement and was working well, but I want to use the While outside of that statement, but I'm getting The name 'file' does not exist in the current context (CS0103) and I don't know how to get the value of file out of my If statement.
You need to get the variable out of the if-scope. Try this:
StreamReader file;
if (File.Exists(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]))
{
file = new StreamReader(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]);
}else{
file = new StreamReader(TestSuite.Current.Parameters["LogPath"]);
}
That way you have a value in file for sure. Happy coding.
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.
I am working on an BB app in which I need to maintain a HTTP connection and with a name of image which is stored on server to get the text written in that image document.
I am getting the response in RTF format.
When I directly hit the server on open browser Chrome, I RTF file get downloaded.
Now I needs to perform that programetically,
1) Either convert the bytes which are coming in response in a simple string format so that I can read that.
or
2) Download the file as its happening on the browser manually so that by reading that file I read the information written in the document.
please suggest me how can I read the data from server by hitting any URL?
Currently I am working with this code:
try {
byte []b = send("new_image.JPG");
String s = new String(b, "UTF-8");
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
public byte[] send(String Imagename) throws Exception
{
HttpConnection hc = null;
String imageName = "BasicExp_1345619462234.jpg";
InputStream is = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] res = null;
try
{
hc = (HttpConnection) Connector.open("http://webservice.tvdevphp.com/basisexpdemo/webservices/ocr.php?imgname="+imageName);
hc.setRequestProperty("Content-Type", "multipart/form-data;");
hc.setRequestMethod(HttpConnection.GET);
int ch;
StringBuffer sb= new StringBuffer();
is = hc.openInputStream();
while ((ch = is.read()) != -1)
{
bos.write(ch);
sb.append(ch);
}
System.out.println(sb.toString());
res = bos.toByteArray();
}
catch(Exception e){
e.printStackTrace();
}
finally
{
try
{
if(bos != null)
bos.close();
if(is != null)
is.close();
if(hc != null)
hc.close();
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
return res;
}
The response is like:
{\rtf1\ansi\ansicpg1252\uc1\deflang1033\adeflang1033...................
I can read the data but its not formatted, so that i can read that programetically too.
I have done with this task....
Actually the mistake was on server side.
When they were performing OCR, the format parameter was not corrected that was reason.
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
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?