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

Which image is clicked

Hi 

i develop a sample application , in which i took all images from sdcard and show add in main screen(in small size). but when i click one of this image i wnt to show this image in full screen  .But i have problem how can i know which image is clicked .? actually i take array in which i take the path of images.

 

Please help

 

 

 

Developer
Posts: 543
Registered: ‎12-23-2010
My Device: Z10
My Carrier: Airtel

Re: Which image is clicked

Can you post your code so that I can help you.

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

Re: Which image is clicked

Can I suggest that you don't post code at the moment.  The problem here is a design question, not a coding question.  You need to decide how you are going to do this, before you start coding.

 

One way is to create a Vector that has a reference to the original SD Card file, let us call this the referenceVector.  Create another Vector that has a reference to the BitmapField that you display, call this the BitmapFieldVector.  As you add a BitmapField to the screen, add an entry in both Vectors.  When you detect an image is clicked, search the BitmapFieldVector to find the matching image.  If this is at element n, then referenceVector element (n) is the associated SD Card image, which you can display enlarged.

 

This is not the most Object friendly approach and there are many other alternatives, but you get the idea. 

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

Re: Which image is clicked

or you can use setCookie to set the reference to the file on the BitmapField.
----------------------------------------------------------
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: Which image is clicked

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;
}
}

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

Re: Which image is clicked

Hi i am using this code this is running code .but problem is that how in know which image is click .please import this class and run.I have to see in full image on next screen when i select any one of the image.
Regular Contributor
Posts: 56
Registered: ‎09-14-2011
My Device: 9000
My Carrier: b.tech

Re: Which image is clicked

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;
}
}

 

 

You  can see my code . please do some modification .