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
Regular Contributor
Posts: 56
Registered: ‎09-14-2011
My Device: 9000
My Carrier: b.tech
Accepted Solution

Getting all images from Devices But How to know in which images is seclected

Hi

in my application i want to show all images in first screen , after selecting any image from all will be seen in next screen (full screen).actually i am getting all images but problem how to know which image is clicked

 

i used this code...............

 

package mypackage;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;

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

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.FlowFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Comparator;
import net.rim.device.api.util.SimpleSortingVector;

/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{

private static final String DEVICE_DIR_PATH = System
.getProperty("fileconn.dir.photos");
private static final String SD_IMAGE_DIR_PATH = System
.getProperty("fileconn.dir.memorycard.photos");
private static final String OS6_CAMERA_PATH = "file:///store/home/user/camera/";
private static final String OS6_SD_CARD_PATH = "file:///SDCard/BlackBerry/camera/";

public static final int DEVICE_AND_SD = 0;
public static final int DEVICE_MEM = 1;
public static final int SD_CARD = 2;
public static final int OS6_CAMERA = 3;
public static final int OS6_SD_CAMERA = 4;
public static final int DEVICE_ALL = 5;
private static String[] _allImagePaths = null;
private EncodedImage[] encodedbitmap;
private Bitmap[] bitmap;
private BitmapField [] imagebitmapField;

int dataSize;
private ButtonField[] bitmapf;

/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");

String[] imagePaths = getImagePaths(DEVICE_ALL);
FlowFieldManager imageFlowField = new FlowFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
if (imagePaths != null && imagePaths.length > 0) {
encodedbitmap = new EncodedImage [imagePaths.length];
bitmap = new Bitmap [imagePaths.length];
imagebitmapField = new BitmapField [imagePaths.length];


for (int i = 0; i < imagePaths.length; i++) {
encodedbitmap[i] = loadEncodedImage(imagePaths[i]);
bitmap[i]=scaleImage(encodedbitmap[i], 200, 100);
imagebitmapField[i] =new BitmapField(bitmap[i],Field.FOCUSABLE);
imageFlowField.add(imagebitmapField[i]);
}
}
add(imageFlowField);


}

public Bitmap scaleImage(EncodedImage img, int width, int height) {

int currentWidthF32 = Fixed32.toFP(img.getWidth());
int currentHeightF32 = Fixed32.toFP(img.getHeight());

if (height == 0) {
int requiredWidth = Fixed32.toFP(width);
int x = Fixed32.div(currentHeightF32, currentWidthF32);
int y = Fixed32.mul(x, requiredWidth);
int scaleX = Fixed32.div(currentWidthF32, requiredWidth);
int scaleY = Fixed32.div(currentHeightF32, y);
img = img.scaleImage32(scaleX, scaleY);
} else {
int currentWidthFixed32 = Fixed32.toFP(img.getWidth());
int currentHeightFixed32 = Fixed32.toFP(img.getHeight());
int requiredHeightFixed32 = Fixed32.toFP(height);
int requiredWidthFixed32 = Fixed32.toFP(width);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
requiredHeightFixed32);
img = img.scaleImage32(scaleXFixed32, scaleYFixed32);

}
return img.getBitmap();
}

protected EncodedImage loadEncodedImage(String filePath) {

FileConnection connection=null;
byte[] byteArray=null;
EncodedImage image = null;
Bitmap bitmap=null;
try
{
connection=(FileConnection)Connector.open(filePath);
if(connection.exists())
{
byteArray=new byte[(int)connection.fileSize()];
InputStream inputStream=connection.openInputStream();
inputStream.read(byteArray);
inputStream.close();
image=EncodedImage.createEncodedImage(byteArray, 0, -1);
}
connection.close();
}
catch(Exception e)
{
System.out.println("Exception "+e.toString());
}
return image;


}


public static String[] getImagePaths(int source) {

// If get path for all do this recursively
if (source == DEVICE_ALL) {
if (_allImagePaths != null) {
return _allImagePaths;
}

// OS6 Device Gallery
String[] os6CameraSDPaths =
getImagePaths(OS6_SD_CAMERA);

// OS6 SD Card Gallery
String[] os6CameraPaths =
getImagePaths(OS6_CAMERA);

// SD Card Gallery
String[] sdCardPaths = getImagePaths(SD_CARD);
// Device Gallery
String[] devicePaths =
getImagePaths(DEVICE_MEM);

// Combine the two into one ImageViewer
int numOS6CameraSDPaths = os6CameraSDPaths != null ? os6CameraSDPaths.length
: 0;
int numOS6CameraPaths = os6CameraPaths != null ? os6CameraPaths.length
: 0;
int numSDCardPaths = sdCardPaths != null ? sdCardPaths.length : 0;
int numDevicePaths = devicePaths != null ? devicePaths.length : 0;

int totalNumPaths = numDevicePaths + numSDCardPaths
+ numOS6CameraPaths + numOS6CameraSDPaths;
if (totalNumPaths > 0) {
String[] paths = new String[totalNumPaths];

if (os6CameraSDPaths != null) {
System.arraycopy(os6CameraSDPaths, 0, paths, 0,
numOS6CameraSDPaths);
}
if (os6CameraPaths != null) {
System.arraycopy(os6CameraPaths, 0, paths,
numOS6CameraSDPaths, numOS6CameraPaths);
}
if (sdCardPaths != null) {
System.arraycopy(sdCardPaths, 0, paths, numOS6CameraSDPaths
+ numOS6CameraPaths, numSDCardPaths);
}
if (devicePaths != null) {
System.arraycopy(devicePaths, 0, paths, numOS6CameraSDPaths
+ numOS6CameraPaths + numSDCardPaths,
numDevicePaths);
}

_allImagePaths = sortPaths(paths);

return _allImagePaths;
} else {
return null;
}
}

// Set the path to look up
String imagePath = DEVICE_DIR_PATH;
if (source == SD_CARD) {
imagePath = SD_IMAGE_DIR_PATH;
}

if (source == OS6_CAMERA) {
imagePath = OS6_CAMERA_PATH;
}

if (source == OS6_SD_CAMERA) {
imagePath = OS6_SD_CARD_PATH;
}

// Enumerated through the directory looking for image files
Vector imagePaths = new Vector();
FileConnection imageDir = null;
try {
imageDir = (FileConnection) Connector.open(imagePath);
if (imageDir != null) {
Enumeration enumeration = imageDir.list();

imageDir.close();
while (enumeration.hasMoreElements()) {
String imageName = (String) enumeration.nextElement();
if (isSupported(imageName)) {
imagePaths.addElement(imagePath + imageName);
}
}
}
} catch (Exception e) {
//XLogger.error(ImagePath.class, "Unable to read folder:"
// + e.getMessage());
} finally {
if (imageDir != null) {
try {
imageDir.close();
} catch (IOException e) {
//XLogger.error(ImagePath.class, "Unable to close folder:"
// + e.getMessage());
}
}
}

// If there are no images return null
if (imagePaths.size() <= 0) {
return null;
}

// Return the results in a String array
_allImagePaths = new String[imagePaths.size()];
imagePaths.copyInto(_allImagePaths);
return _allImagePaths;
}


private static String[] sortPaths(String[] paths) {
// Sort the paths by the date/time the files where modified.
class PathAndLastModified {

private long timeStamp;
private String path;

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}

public void setPath(String path) {
this.path = path;
}

public String getPath() {
return path;
}
}

// Comparator to sort paths
Comparator pathComparator = new Comparator() {
public int compare(Object o1, Object o2) {
if( ((PathAndLastModified)o1).timeStamp < ((PathAndLastModified)o2).timeStamp ) {
return 1;
} else if( ((PathAndLastModified)o1).timeStamp > ((PathAndLastModified)o2).timeStamp){
return -1;
} else {
return 0;
}

// return (int) (((PathAndLastModified)o2).timeStamp - ((PathAndLastModified)o1).timeStamp);
}
};

// Add paths to the sorting vector
SimpleSortingVector sortedPaths = new SimpleSortingVector();
sortedPaths.setSortComparator(pathComparator);
for (int i = 0; i < paths.length; i++) {
String filePath = paths[i];
long lastModified = 0;

FileConnection baseConn = null;
try {
baseConn = (FileConnection) Connector.open(paths[i]);
lastModified = baseConn.lastModified();

PathAndLastModified pathAndLastModified = new PathAndLastModified();
pathAndLastModified.setPath(filePath);
pathAndLastModified.setTimeStamp(lastModified);

sortedPaths.addElement(pathAndLastModified);
} catch (Exception e) {
// Do nothing
} finally {
if(baseConn!=null) {
try {
baseConn.close();
} catch (IOException e) {
// Do nothing
}
}
}
}
sortedPaths.reSort();

// Iterate through to create an array of sorted paths
String[] sortedArray = new String[sortedPaths.size()];
for(int i =0; i <sortedPaths.size(); i++ ) {
sortedArray[i] = ((PathAndLastModified)sortedPaths.elementAt(i)).getPath();
}

return sortedArray;
}


private static boolean isSupported(String image) {
// make sure image is of appropriate type
if (image == null) {
return false;
}
// directory check
if (image.indexOf("/") >= 0) {
return false;
}

int delimiterIndex = image.indexOf(".");
if (delimiterIndex == -1) {
// probably a directory
return false;
}

// check against a list of supported extensions
String[] extensions = { ".jpg", ".jpeg" };
for (int i = 0; i < extensions.length; i++) {
if (image.length() > extensions[i].length()) {
String ext = image.substring(image.length()
- extensions[i].length());
if (ext.equalsIgnoreCase(extensions[i])) {
return true;
}
}
}
return false;
}
}

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Getting all images from Devices But How to know in which images is seclected

please limit your code to the relevant parts and format it properly.

you are using bitmapfields that are focusable, but that does not mean that they handle any user input.
you can overwrite navigationclick and use getLeafFieldWithFocus to get the clicked bitmapfield.

i can also suggest that you do not execute file operations on the event thread, use a separate thread.
see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

you can also split code into several classes, this may make it easier to understand, not only for us but also for you.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
Posts: 56
Registered: ‎09-14-2011
My Device: 9000
My Carrier: b.tech

Re: Getting all images from Devices But How to know in which images is seclected

for (int i = 0; i < imagePaths.length; i++) {
encodedbitmap[i] = loadEncodedImage(imagePaths[i]);
bitmap[i] = scaleImage(encodedbitmap[i], 200, 100);
imagebitmapField[i] = new BitmapField(bitmap[i],
Field.FOCUSABLE){
protected boolean navigationClick(int arg0, int arg1) {

BitmapField biFiel=(BitmapField)getLeafFieldWithFocus();
UiApplication.getUiApplication().pushScreen(new test(biFiel));
return false;


};
};
imageFlowField.add(imagebitmapField[i]);
}
}


I added this ........in test screen simply add but getting error

public test(final BitmapField bitmapField) {
add(bitmapField);



}
Regular Contributor
Posts: 56
Registered: ‎09-14-2011
My Device: 9000
My Carrier: b.tech

Re: Getting all images from Devices But How to know in which images is seclected

Thanks sir but i am making sample app and if i generate separate class then it is difficult to past code thank for reply but getting error
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Getting all images from Devices But How to know in which images is seclected

my suggestion was meant to overwrite navigationclick on the screen.
if you want to override it for the field you don't have to use getLeafFieldWithFocus.

i would suggest that you think your code through, don't write stuff and try to get it to work, fist think about something and then implement it.
our projects at university were usually 80% design, 20% coding - it was strange at first, but it produced much better code.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
Posts: 56
Registered: ‎09-14-2011
My Device: 9000
My Carrier: b.tech

Re: Getting all images from Devices But How to know in which images is seclected

Thanks Got result 

 

 

Thanks for reply 

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

Re: Getting all images from Devices But How to know in which images is selected

Good, can you please mark this and your other related posts as solved?  Instructions in Help link near top of screen if you need them.