Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified
Accepted Solution

SDcard connection issue

I'm using a connection to connect to my SDcard and see if there is an image. If not the script creates the image, if so it will delete the image and then create it. The script works fine when ran the first few times. After that though it seems as if it will not close the connection to the SDcard.

 

It's stops wirting / reading the card. My device still sees the card as being in the device but I have no way to aceess it. When I open the card up there are no files or anything on it. The device says the card is blank. If I remove the card though and put it back in everything works with it again. Here is the code I'm using can you guys see anything wrong with it?

 

 

 

            String path = "file:///SDCard/BlackBerry/pictures/sw.jpg";
            FileConnection FileConn = null; 
            try{
                FileConn = (FileConnection) Connector.open(path,Connector.READ_WRITE); 
                if(!FileConn.exists()){
                    FileConn.create();  
                }
                FileConn.setWritable(true);
                PNGEncoder encoder = new PNGEncoder(bg);           
                java.io.OutputStream output = FileConn.openOutputStream();
                output.write(encoder.encode(true));
                output.close();
                HomeScreen.setBackgroundImage(path);
            } catch(Exception e){
                System.out.println("Image Error: " + e);
            } finally {
                try{ FileConn.close(); } catch(Exception e){}
            }

 

 

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: SDcard connection issue

Just a guess, maybe try calling setBackgroundImage after closing FileCon. Also, just as a opinion, to make everything "fit together" better the path should end with png (don't think that would cause any issues with setBackgroundImage but don't know) and setWritable is not needed unless there is a chance that the image/path is readonly.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: SDcard connection issue

 


rcmaniac25 wrote:

Just a guess, maybe try calling setBackgroundImage after closing FileCon. Also, just as a opinion, to make everything "fit together" better the path should end with png (don't think that would cause any issues with setBackgroundImage but don't know) and setWritable is not needed unless there is a chance that the image/path is readonly.


 

 

Okay I tried calling the setBackground after the connection closes. I also removed the setWritable and changed the jpg to png.

 

Everything works just fine, but after like 2-3 hours the SDcard starts acting up again. I'm not sure what is causing it but for some reason it's not being closed. Does anyone else have any suggestions?

 

Thanks

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: SDcard connection issue

Wait, it works for some time then stops working? Are you testing this on a real device or the simulator?

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: SDcard connection issue

 


rcmaniac25 wrote:

Wait, it works for some time then stops working? Are you testing this on a real device or the simulator?


 

 

Yes the code works just fine for a while but then quits.

 

I have this code run in the background every 15,30,45 or 60 minutes (ther user can pick how often it updates). Anyways it will update just fine for like an hour or so and then for some reason the SDcard will not respond after that. I'm using this on the real device not the simulator. It seems to me like the SDcard connection never gets closed for some reason and then becomes unavailable. I'm not sure what it is but I can't seem to get any errors or anything like that on the simulator.

 

Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: SDcard connection issue

Okay I found the issue. I had an inputstream that I forgot to close porperly. So after X amount of times running the app wouldn't allow me to open anymore streams. I closed it properly and everything is running smooth now.

Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: SDcard connection issue

Good, if your issue is resolved then mark whatever post helped you (if any) as the solution so anyone else that has this issue can see what fixed the issue.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.