FileIOException: File already exists error on blackberry - blackberry

i get thumbnail image using fileconnection.
I get image using thread constructor. I pass the url and get image.
If the two image url are same , i got exception "FileIOException: File already exists "
My code is here.,
FileConnection fConn = null;
try
{
String fileString = MD5.encodeStringMD5(url);
fConn = (FileConnection) Connector.open(fileTempPath+fileString+".png");
if(!fConn.exists())
{
fConn.create();
GetImageFromURL(url,fConn,id);
}
else
{
GetImageFromFolder(fConn, id);
}
fConn.close();
}
catch (Exception e)
{
System.out.println("------"+e);
}
If the urls are differ. No problem occur. But if two or three url r same , Only one image only stored and load on screen. others same url not displaying.
After stored on device memory, its loadding all image.
The Exception throws in this line - "fConn.create();"

Before creating a new file, try to open a file with the same name/path. If it already exists remove it.

If file already exits then do like this:
if(!fConn.exists())
{
fConn.create();
GetImageFromURL(url,fConn,id);
}
else
{
fConn.truncate();//it removes the data in that file;
GetImageFromFolder(url,fConn, id);
}

Related

Download file Server Error: The handle is invalid?

public ActionResult FileLink(string hashname)
{
try
{
const string basePath = #"\\WINDHOVERDOCUMENTS\";
const string adminSamples = #"Beta\students\";
return File(basePath + adminSamples + hashname, "application/force-download", hashname);
}
catch (Exception)
{
return null; //no file
}
}
This action simple force user to download the file when the action is triggered. Everything works fine locally. But after publishing to server, it gives me this error. Below is the screenshot. Can anyone help? Thank you. please zoom in to see the screenshot. Sorry.
I solved that by reading the file to byte array then return file content result
var fileBytes = System.IO.File.ReadAllBytes(#"\\path\fileP12.zip");
return File(fileBytes, "application/zip", "package.zip");

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 get a Image from web url without lossing its properties in blackberry

i am downloading an image from url from my application using http post method. after that how to save that with out losing its properties like transference?
thank you
That's how you save an image to the device:
try {
FileConnection fconn = (FileConnection) Connector.open(filePath,Connector.READ_WRITE);
fconn.create();
out = fconn.openOutputStream();
out.write(yourImageBytes);
out.flush();
fconn.close();
} catch (Exception ex) { }

"A generic error occurred in GDI+" error while showing uploaded images

i am using the following code to show the image that has been saved in my database from my asp.net mvc(C#) application:.
public ActionResult GetSiteHeaderLogo()
{
SiteHeader _siteHeader = new SiteHeader();
Image imgImage = null;
long userId = Utility.GetUserIdFromSession();
if (userId > 0)
{
_siteHeader = this.siteBLL.GetSiteHeaderLogo(userId);
if (_siteHeader.Logo != null && _siteHeader.Logo.Length > 0)
{
byte[] _imageBytes = _siteHeader.Logo;
if (_imageBytes != null)
{
using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes))
{
imgImage = Image.FromStream(imageStream);
}
}
string sFileExtension = _siteHeader.FileName.Substring(_siteHeader.FileName.IndexOf('.') + 1,
_siteHeader.FileName.Length - (_siteHeader.FileName.IndexOf('.') + 1));
Response.ContentType = Utility.GetContentTypeByExtension(sFileExtension.ToLower());
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BufferOutput = false;
if (imgImage != null)
{
ImageFormat _imageFormat = Utility.GetImageFormat(sFileExtension.ToLower());
imgImage.Save(Response.OutputStream, _imageFormat);
imgImage.Dispose();
}
}
}
return new EmptyResult();
}
It works fine when i upload original image. But when i upload any downloaded images, it throws the following error:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
For. Ex: When i upload the original image, it shows as logo in my site and i downloaded that logo from the site and when i re-upload the same downloaded image, it throws the above error. It seems very weird to me and not able to find why its happening. Any ideas on this?
I'd guess that your problem lies here:
using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes))
{
imgImage = Image.FromStream(imageStream);
}
Because after using .FromStream, the Image owns the stream and might be very upset if you close it. To verify if that's the problem you can just try:
using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes))
{
imgImage = new Bitmap( Image.FromStream(imageStream) );
}
I've found that error usually comes from a file access problem. Sounds obvious, I realize, but double-check that the file path is correct and that the file exists, and also that the IIS process has permissions to that file.

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