×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Failed to open image that save using FileConnection

    Hi,
    i want to ask something...
    my j2me app is open a camera and take picture and than saving it..
    for the saving section i am using code example in the wiki part..
    when i try to save image.. the saving is successful..
    for example: i succeed saving image "savedImage.jpg" with size about 150kB, but when i want to open that image, it cause error "file format not supported" or something like that..
    can any one help me how to fix this?? i've try saving image to jpg, and png, both cause the same error..

    This my saving code:
    Code:
    private class SaveImage extends Thread
    	{
    		String FILE_Dir = "file:///E:/Images/";
    		  /** The base file name used to store pictures */
    	   String FILE_NAME = "SavedImage";
    
    	    /** The extension of the pictures to be saved */
    	   String EXTENSION = ".png";
    
    
    	    /** A counter for the number of snapshots taken */
    	    int _counter;
    	    byte[] data=null;
    
    		public void run() {
    			
    			loading = new Alert("Loading","Saving image", null, AlertType.INFO);
    			
    			Gauge a = new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
    			loading.setIndicator(a);
    			loading.setTimeout(100000);
    			MainMidlet.display.setCurrent(loading, screen);
    
    			FileConnection file=null;
    			OutputStream out=null;
    			 try
    	            {       
    				 	data = ImageProcessing.get_Byte_Array(mainImg);
    				 
    	                // Create the connection to a file that may or
    	                // may not exist.
    	                file = (FileConnection)Connector.open(FILE_Dir+ FILE_NAME+ EXTENSION );
    
    	                // If the file exists, increment the counter until we find
    	                // one that hasn't been created yet.
    	                while( file.exists() )
    	                {
    	                    file.close();
    	                    ++_counter;
    	                    file = (FileConnection)Connector.open( FILE_Dir+FILE_NAME + _counter + EXTENSION );
    	                }
    
    	                System.out.println(FILE_Dir+FILE_NAME + _counter+ EXTENSION);
    	                // We know the file doesn't exist yet, so create it
    	                file.create();
    
    	                // Write the image to the file
    	                out = file.openOutputStream();
    	                out.write(data);
    	                
    	                
    
    	                // Close the connections
    	                out.close();
    	                file.close();
    	                Image temp = ImageProcessing.resizeImage(mainImg, 90, 120);
    	                SavedImage save = new SavedImage(temp, System.currentTimeMillis(), FILE_NAME + _counter + EXTENSION);
    	                Vector savedImage = new SavedImageBean().loadSavedImage();
    	                
    	                if(savedImage.size()==5)
    	                {
    	                	savedImage.removeElementAt(0);
    	                }
    	                savedImage.addElement(save);
    	                System.out.println("PANJANG VECTOR ="+ savedImage.size());
    	                new SavedImageBean().clearFavorit();
    	                new SavedImageBean().saveAllSavedImage(savedImage);
    	                MainMidlet.display.setCurrent( screen);
    	                showAlert("Information", "Image has been saved to "+FILE_Dir, screen);
    
    	            }
    	            catch(Exception e)
    	            {
    	            	MainMidlet.display.setCurrent(screen);
    	            	showAlert("Error", "Failed to save image "+e.toString(), screen);
    	            }
    	            finally {
    	                // Try to close file
    	                try {
    	                    if(out != null) {
    	                        out.close();
    	                    }
    	                    if(file != null) {
    	                        file.close();
    	                    }
    	                } catch(Exception exc) {
    	                    // Do nothing 
    	                }
    	            }
    		}
    	}
    please help...
    and how to save image to folder that doesnt exist before... if i save image to "file:///E:/Images/" it always succeed but always failed if i try to save at "file:///E:/Images/Testing"

    Thanks...

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Failed to open image that save using FileConnection

    Hi elisafina,

    Try using these snippets:

    For reading your file and creating an Image object so that you can display it on the screen:
    Code:
    			FileConnection filecon=(FileConnection)Connector.open("file:///E:/Testapp/image.jpg",Connector.READ);			
    			int fsize=(int)filecon.fileSize();
    			InputStream fis=filecon.openInputStream();
    			byte[] imgdata=new byte[fsize];			
    			fis.read(imgdata);
    			//Image image=Image.createImage(fis);
    			fis.close();
    			filecon.close();
    For writing a file provided that you have its byte array imgdata available in memory:
    Code:
    			FileConnection w=(FileConnection) Connector.open("file:///E:/Testapp1/",Connector.READ_WRITE);			
    			w.mkdir();
    			w.close();
    			
    			FileConnection wf=(FileConnection) Connector.open("file:///E:/Testapp1/image1.jpg",Connector.READ_WRITE);
    			if(!wf.exists())
    			{
    				wf.create();
    				System.out.println("Created!");
    			}
    			else
    			{
    				System.out.println("Already exists!");
    			}
    			OutputStream fos=wf.openOutputStream();
    			fos.write(imgdata);
    			fos.flush();
    			fos.close();
    			wf.close();
    You can create a directory that doesn't exist with the mkdir() method as shown above and a file that doesn't exist with the create() method.

    You need to place the above snippets within a try catch argument.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Re: Failed to open image that save using FileConnection

    Wow... many thanks skalogir.. i'll try it ASAP...

    but, when i say i want to open saved image what i mean is, i try to open it by phone file browser, not using any app..
    and i figured why i can't open saved image.. that because i didn't use JPGEncoder.. once i add JPGEncoder library, the saved image now can be displayed..

    Thanks a lot

Similar Threads

  1. FileConnection.write() STRANGE behaviour, trying to save image!!!
    By sandro1972 in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2008-01-05, 09:48
  2. Replies: 7
    Last Post: 2007-10-02, 14:59
  3. FileConnection :: File Con't open
    By saifulhq in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2007-03-11, 01:15
  4. FileConnection open returns IOException
    By browndrf in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2006-07-18, 21:49

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×