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: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN
Accepted Solution

FilePicker issues

Hello and good day , 

I have been finding it difficult to implement the filepicker api , it has been a nightmare , i am wondering why , i am developing an app for os5.0 and above , but i find it difficult to implement the file picker i have tried everything possible , the situation is this i have a bitmapfield and i want to be able to click on it to display a filepicker which i have done , when the filepicker is  displayed i want the user to select an image and once the image has been selected , the selected image should be set on the bitmapfield , i have tried it and i get funny behaviors on both os 5.0 and 6.0 simulators here is my sample code 

 

UiApplication.getUiApplication().invokeLater(new Runnable(){

public void run() {
// TODO Auto-generated method stub

final FilePicker filePicker;
filePicker=FilePicker.getInstance();
filePicker.setPath(getSelectedPhotoPath());
filePicker.show();

filePicker.setListener(new Listener()
{

public void selectionDone(String path)
{
profile_pic.setBitmap(getTheImage(path));




}
});

 

/**
* retrieve the picture path based on the availability of SDCard
* @return
*/
public String getSelectedPhotoPath()
{


String path = null;
boolean val = Utilities.isSDCardAvailable();
if(val)
{
path= "file:///SDCard/BlackBerry/pictures/";
}
else
{
path= "file:///store/home/user/pictures/";
}

return path ;

}

 

 

/**
*
* return the image that will be used as user profile
* @param url
* @return
*/
private Bitmap getTheImage(String url)
{
Bitmap bitmap=null,scaleBitmap=null;
InputStream inputStream=null;
FileConnection fileConnection=null;
try
{
fileConnection=(FileConnection) Connector.open(url, Connector.READ_WRITE);
inputStream = fileConnection.openInputStream();
byte[] data =new byte[(int)fileConnection.fileSize()];
data=IOUtilities.streamToBytes(inputStream);

inputStream.close();
fileConnection.close();
bitmap=Bitmap.createBitmapFromBytes(data,0,data.length,1);

//You can return this bitmap otherwise, after this you can scale it according to your requirement; like...
scaleBitmap=new Bitmap(50, 50);
bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
}
catch (Exception e)
{
try
{
if(inputStream!=null)
{
inputStream.close();
}
if(fileConnection!=null)
{
fileConnection.close();
}
}
catch (Exception exp)
{
Dialog.alert(exp.getMessage());
}
scaleBitmap=Bitmap.getBitmapResource("avatar.png");//Your known Image;
}
return scaleBitmap;
}

 

Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: FilePicker issues

"i get funny behaviors on both os 5.0 and 6.0 simulators "

 

Please explain the behaviours you see that you are not expecting.

Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: FilePicker issues

Once the file is selected the filepicker does not close , I have even tried calling filepicker.cancel() on the selectDone interface method and I still face the same issue , it is totally weird . The filepicker still remains on the screen without closing after selection
Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: FilePicker issues

Just had a quick look at your code, - wouldn't it be more sensible to set the Listener before you show the FilePicker Dialog?  Perhaps that is your problem?  Is the selectDone() even invoked?  What happens when you debug this?

Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: FilePicker issues

I have placed the show method after the listener and I still have the same issue , I wonder why is this a filepicker api bug like I read in some forums .
Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: FilePicker issues

May we see the updated code ?

 

Is your selectionDone(() method actually invoked?

Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: FilePicker issues

Yes it is , I noticed that the problem is from when I try to replace the bitmap in my custom bitmapfield with the selected bitmap from the filepicker , that's where the problem starts , I even tried removing my custom bitmap field from its manager and creating a new bitmapfield with the new bitmap and adding back to the manager it worked but I had issues with the event listener for my bitmapfield since when u click on the bitmapfield the filepicker is invoked , the event listener is not registered with the newly created bitmapfield
Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: FilePicker issues

I am unclear now as to whether you are looking for help in your FIlePicker of your change listener or in fact if you have resolved the problem.  Can you please clarify?

Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: FilePicker issues

Here is my code 

package carniriv;

import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.picker.FilePicker;
import carniriv.components.ui.CarniRivScreen;
import carniriv.components.ui.CustomButton;
import carniriv.components.ui.ImageButtonField;
import carniriv.components.ui.LabeledEmailField;
import carniriv.components.ui.LabeledPasswordField;
import carniriv.components.ui.LabeledTextField;
import carniriv.components.ui.TopBanner;
import carniriv.utilities.Utilities;

/**
*
* screen to handle registeration of user profile
* @author insolsoft-lab
*
*/
public class RegisterScreen extends CarniRivScreen implements FieldChangeListener
{
TopBanner banner ;
VerticalFieldManager form_container = new VerticalFieldManager(VERTICAL_SCROLLBAR |VERTICAL_SCROLL);
LabeledTextField user_id = new LabeledTextField("User ID*", DrawStyle.HCENTER |FIELD_HCENTER, "Required*");
LabeledPasswordField password = new LabeledPasswordField("Password*", DrawStyle.HCENTER |FIELD_HCENTER, "");
LabeledEmailField email = new LabeledEmailField("Email*", DrawStyle.HCENTER |FIELD_HCENTER, "Required*");
LabeledTextField phone_no = new LabeledTextField("Phone No*", DrawStyle.HCENTER |FIELD_HCENTER, "Required*");
HorizontalFieldManager button_container = new HorizontalFieldManager(FIELD_HCENTER);
HorizontalFieldManager profile_container = new HorizontalFieldManager(Field.FIELD_HCENTER|Field.FIELD_VCENTER);

 

// my custom bitmapfield
ImageButtonField profile_pic = new ImageButtonField(Bitmap.getBitmapResource("avatar.png"),Field.FIELD_VCENTER);


CustomButton save = new CustomButton("Save Profile", DrawStyle.HCENTER |FIELD_HCENTER);


public RegisterScreen()
{
super(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR);
// TODO Auto-generated constructor stub
banner = new TopBanner(USE_ALL_WIDTH, "My Profile");
add(banner);
//add(new SeparatorField());

add(new LabelField(""));

profile_container.add(profile_pic);



form_container.add(profile_container);
form_container.add(new LabelField(""));
form_container.add(user_id);
form_container.add(password);
form_container.add(email);
form_container.add(phone_no);

button_container.add(save);
form_container.add(button_container);
add(form_container);

profile_pic.setChangeListener(this);

}

/**
*
* return the image that will be used as user profile
* @param url
* @return
*/
private Bitmap getTheImage(String url)
{
Bitmap bitmap=null,scaleBitmap=null;
InputStream inputStream=null;
FileConnection fileConnection=null;
try
{
fileConnection=(FileConnection) Connector.open(url, Connector.READ_WRITE);
inputStream = fileConnection.openInputStream();
byte[] data =new byte[(int)fileConnection.fileSize()];
data=IOUtilities.streamToBytes(inputStream);

inputStream.close();
fileConnection.close();
bitmap=Bitmap.createBitmapFromBytes(data,0,data.length,1);

//You can return this bitmap otherwise, after this you can scale it according to your requirement; like...
scaleBitmap=new Bitmap(50, 50);
bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
}
catch (Exception e)
{
try
{
if(inputStream!=null)
{
inputStream.close();
}
if(fileConnection!=null)
{
fileConnection.close();
}
}
catch (Exception exp)
{
Dialog.alert(exp.getMessage());
}
scaleBitmap=Bitmap.getBitmapResource("avatar.png");//Your known Image;
}
return scaleBitmap;
}


public void fieldChanged(Field field, int arg1)
{
// TODO Auto-generated method stub
if(field == profile_pic)
{
try
{

UiApplication.getUiApplication().invokeLater(new Runnable()
{


public void run()
{
// TODO Auto-generated method stub
FilePicker filePicker = FilePicker.getInstance();
filePicker.setPath(getSelectedPhotoPath());
filePicker.setListener(new FilePickListener());
filePicker.show();







}
});

}
//it show what ever you select.

catch (Throwable e)
{
profile_pic.setBitmap(Bitmap.getBitmapResource("avatar.png"));
Dialog.alert("Error"+ e.getMessage());
}


}


}// end of field changed

/**
* retrieve the picture path based on the availability of SDCard
* @return
*/
public String getSelectedPhotoPath()
{


String path = null;
boolean val = Utilities.isSDCardAvailable();
if(val)
{
path= "file:///SDCard/BlackBerry/pictures/";
}
else
{
path= "file:///store/home/user/pictures/";
}

return path ;

}

/**
*
* class for filepicker listener
* @author insolsoft-lab
*
*/
class FilePickListener implements FilePicker.Listener
{
public void selectionDone(String path) //invoked when user has selected a file
{
Bitmap bmp = getTheImage(path);

profile_container.delete(profile_pic);

 

// this part is where the problem is when i try to replace the  existing bitmap with the selected bitmap from filepicker  
profile_pic.setBitmap(bmp);

// profile_container.add(new ImageButtonField(bmp, Field.FIELD_VCENTER));
invalidate();





}
}

}

Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: FilePicker issues

[ Edited ]

You should be able to just update the BitmapField with the new Bitmap rather than replace the whole Field.  That will resolve the problme with the Listener changing. 

 

Can I recommend that you describe your problem separately from your code.  Also give as much detail as you can.  What happens?  Exceptions thrown?  What do you expect to happen?