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
New Developer
adem666
Posts: 103
Registered: ‎05-05-2009
My Device: Not Specified
Accepted Solution

How can I add image to galery and view it from BlackBerry Media Center

I want to add a file, like image, to device file system. then, view it from BlackBerry media center. I use below folder to add an image 

 

System.getProperty("fileconn.dir.photos"); 

 

this function gives to me a path and I write to file to this folder. But, after I write, I wouldnot see from BlackBerry native media center. What do I do, to see this image ?

 

Thanks 

Please use plain text.
Administrator
MSohm
Posts: 14,410
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: How can I add image to gallery and view it from BlackBerry Media Center

The BlackBerry Media Center should be able to display all folders.  Are you selecting the view all photos option?  What folder are you writing to?  Have you tried a different folder?  What format is the image?  What BlackBerry Smartphone model and BlackBerry handheld software version are you testing on?  You can find this under Options, About on the BlackBerry Smartphone.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Developer
adem666
Posts: 103
Registered: ‎05-05-2009
My Device: Not Specified

Re: How can I add image to gallery and view it from BlackBerry Media Center

[ Edited ]

I made some mistakes. I tried to view galery's pictures' thumbnails. Other J2ME devices uses EXIF and pictures have thumbnail on itself. but, BlackBerry's picture (taken from camera) does not have thumbnail. otherwise, there is a file in images folder. BBThumbs.dat file. it keeps thumbnails of all galery images. I made a decoder. maybe somebody needs it like me. I paste my code here.

 

 

 

private Hashtable thumbsMap; public byte[] getThumb(String fileName){ String DB = fileName.substring(0,fileName.lastIndexOf('/'))+"/BBThumbs.dat"; fileName = fileName.substring(fileName.lastIndexOf('/')+1,fileName.length()); boolean first=false; if (thumbsMap == null){ first=true; thumbsMap = new Hashtable(); } byte b[] = null; FileConnection fc=null; InputStream in = null; try { fc = (FileConnection)Connector.open(DB,Connector.READ); if (!fc.exists()){ return null; } in = fc.openInputStream(); b = getThumbNail(first, thumbsMap, fileName, in); } catch (Exception e) { } finally { if (fc != null){ try { fc.close(); } catch (Exception e) { } } if (in != null){ try { in.close(); } catch (Exception e) { } } } return b; }private byte[] getThumbNail(boolean firstTime,Hashtable map, String fileName,InputStream thumbDBStream) throws Exception { if (!firstTime){ int coors[] = (int[])map.get(fileName); if (coors == null){ return null; } int fileStart = coors[0]; int fileLength= coors[1]; while ( (fileStart = fileStart - (int)thumbDBStream.skip(fileStart)) > 0); byte out[] = new byte[fileLength]; int p = out.length; while ( (p = p - thumbDBStream.read( out,out.length - p , p)) > 0 ); // int c = 0;// int b_ = in.read();// while (b_ != -1){// if (c >= fileStart ){// out[c - fileStart ] = (byte)b_;// }// c++;// if (c == fileStart + fileLength){// in.close();// return out;// }// b_ = in.read();// } return out; } byte png_header[] = { (byte)0x89,0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; int png_header_count=0; int b_=thumbDBStream.read(); int count =0; ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte out[] = null; boolean firstThumb = true; int fileCopyPosition = 0; boolean fileFound = false; boolean searchHeader=true; int lastThumbLength = 0; int lastThumbPosition = 0; while (b_!= -1){ if (searchHeader){ bo.write(b_); if ((byte)b_ == png_header[png_header_count]){ png_header_count++; if (png_header_count == png_header.length){ png_header_count=0; byte bo_[]=bo.toByteArray(); int nameLengt=0; int pos = 0; if (firstThumb){ nameLengt = (0xff00&(bo_[6]<<8))|(bo_[7]&0xff); pos = 8; } else { nameLengt = (0xff00&(bo_[51]<<8))|(bo_[52]&0xff); pos = 53; } String name = new String(bo_,pos,nameLengt); //printbytes(bo_); pos += nameLengt; pos += 18; lastThumbLength = ((bo_[pos]<<16)&0xff0000)| ((bo_[pos+1]<<8)&0xff00)|(bo_[pos+2]&0xff); lastThumbPosition = count - png_header.length+1; map.put(name, new int[]{lastThumbPosition,lastThumbLength}); bo = new ByteArrayOutputStream(); firstThumb = false; if (name.equals(fileName)){ fileFound = true; out = new byte[lastThumbLength]; System.arraycopy(png_header, 0, out, 0, png_header.length); fileCopyPosition = png_header.length; fileFound = true; } else { fileFound = false; } searchHeader = false; lastThumbPosition = count-png_header.length; } } else { png_header_count=0; } } else { if (fileFound) { out[fileCopyPosition] = (byte)b_; fileCopyPosition++; } } if (lastThumbLength + lastThumbPosition == count){ searchHeader = true; } count++; b_=thumbDBStream.read(); } return out; }

 

 

 

Message Edited by adem666 on 09-18-2009 11:23 AM
Message Edited by adem666 on 09-18-2009 11:24 AM
Please use plain text.
New Developer
sagare
Posts: 86
Registered: ‎05-13-2009
My Device: Not Specified

Re: How can I add image to gallery and view it from BlackBerry Media Center

Hi adem666,

 

           I also wanted to show the thumbnails of all the gallery images in my application can u plz help me with this i tried to run ur code but it has some compile time exception can u plz copy paste it again with proper format. Also i wanted to make sure whether all device do have this .dat file that u r mentioning i.e. can i port this code for all device is what i dont know can u plz help me with this?

 

 

Thanks a lot,

Sagar

Please use plain text.
New Developer
adem666
Posts: 103
Registered: ‎05-05-2009
My Device: Not Specified

Re: How can I add image to galery and view it from BlackBerry Media Center

I hope our incredible forum show true line breaks.

 

import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.Hashtable; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; public class ThumbDBCreator { private Hashtable thumbsMap; public byte[] getThumb(String fileName) { String DB = fileName.substring(0, fileName.lastIndexOf('/')) + "/BBThumbs.dat"; fileName = fileName.substring(fileName.lastIndexOf('/') + 1, fileName .length()); boolean first = false; if (thumbsMap == null) { first = true; thumbsMap = new Hashtable(); } byte b[] = null; FileConnection fc = null; InputStream in = null; try { fc = (FileConnection) Connector.open(DB, Connector.READ); if (!fc.exists()) { return null; } in = fc.openInputStream(); b = getThumbNail(first, thumbsMap, fileName, in); } catch (Exception e) { } finally { if (fc != null) { try { fc.close(); } catch (Exception e) { } } if (in != null) { try { in.close(); } catch (Exception e) { } } } return b; } private byte[] getThumbNail(boolean firstTime, Hashtable map, String fileName, InputStream thumbDBStream) throws Exception { if (!firstTime) { int coors[] = (int[]) map.get(fileName); if (coors == null) { return null; } int fileStart = coors[0]; int fileLength = coors[1]; while ((fileStart = fileStart - (int) thumbDBStream.skip(fileStart)) > 0) ; byte out[] = new byte[fileLength]; int p = out.length; while ((p = p - thumbDBStream.read(out, out.length - p, p)) > 0) ; return out; } byte png_header[] = { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; int png_header_count = 0; int b_ = thumbDBStream.read(); int count = 0; ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte out[] = null; boolean firstThumb = true; int fileCopyPosition = 0; boolean fileFound = false; boolean searchHeader = true; int lastThumbLength = 0; int lastThumbPosition = 0; while (b_ != -1) { if (searchHeader) { bo.write(b_); if ((byte) b_ == png_header[png_header_count]) { png_header_count++; if (png_header_count == png_header.length) { png_header_count = 0; byte bo_[] = bo.toByteArray(); int nameLengt = 0; int pos = 0; if (firstThumb) { nameLengt = (0xff00 & (bo_[6] << 8)) | (bo_[7] & 0xff); pos = 8; } else { nameLengt = (0xff00 & (bo_[51] << 8)) | (bo_[52] & 0xff); pos = 53; } String name = new String(bo_, pos, nameLengt); // printbytes(bo_); pos += nameLengt; pos += 18; lastThumbLength = ((bo_[pos] << 16) & 0xff0000) | ((bo_[pos + 1] << 8) & 0xff00) | (bo_[pos + 2] & 0xff); lastThumbPosition = count - png_header.length + 1; map.put(name, new int[] { lastThumbPosition, lastThumbLength }); bo = new ByteArrayOutputStream(); firstThumb = false; if (name.equals(fileName)) { fileFound = true; out = new byte[lastThumbLength]; System.arraycopy(png_header, 0, out, 0, png_header.length); fileCopyPosition = png_header.length; fileFound = true; } else { fileFound = false; } searchHeader = false; lastThumbPosition = count - png_header.length; } } else { png_header_count = 0; } } else { if (fileFound) { out[fileCopyPosition] = (byte) b_; fileCopyPosition++; } } if (lastThumbLength + lastThumbPosition == count) { searchHeader = true; } count++; b_ = thumbDBStream.read(); } return out; } }

 

Please use plain text.
New Developer
sagare
Posts: 86
Registered: ‎05-13-2009
My Device: Not Specified

Re: How can I add image to galery and view it from BlackBerry Media Center

Hi adem666,

 

Thanks a lot for the reply. Now the code compiles fine without any error. I use the class u provided as follows for getting a byte array of thumbnail of a specific image in pictures folder

 

            ThumbDBCreator thumbCreater = new ThumbDBCreator();
            byte[] thumb = thumbCreater.getThumb("file:///store/home/user/pictures/mypic.jpg");
           
            System.out.println("thumb is *********************************************" + thumb);
            if(thumb != null) {
               
                EncodedImage encodedImage = EncodedImage.createEncodedImage(thumb , 0, -1);
                BitmapField b =  new BitmapField(encodedImage.getBitmap());
                this.add(b);
            }

 

But i get the byte array as null always on 8900 emulator when i do actually have an image names mypic in the pictures folder

 

Can u plz help me in this regard also i dont see the BBThumbs.dat file in any of the pictures folder on emulator atleast.

 

 

Please help me with this.

 

Thanks,

Sagar

Please use plain text.
New Developer
adem666
Posts: 103
Registered: ‎05-05-2009
My Device: Not Specified

Re: How can I add image to galery and view it from BlackBerry Media Center

if you browse device image galery, then BlackBerry create BBthumbs.dat automaticly. So, if you get thumbs from BBThumbs.dat, you browse image galery before start application. I dont know how to start thumbs creator. so if you get null, you should create thumbs from EncodedImage resize function.

 

in my code, firstly I decode all thumbs names. you can see them from hashtable of map. it keeps, thumbs names, and their start byte and lenght. 

Please use plain text.
New Contributor
alpapatel
Posts: 5
Registered: ‎11-14-2010
My Device: Not Specified
My Carrier: Software Engineer

Re: How can I add image to galery and view it from BlackBerry Media Center

Did it work for 8900? I tried same code, I used 8120, simulator and it worked but when i tried on 8900, real device it did not work.

 

I am trying to understand whether my code is missing something or I need to change my approach to solve this problem?

 

Thanks in advance!

Alpa

Please use plain text.
New Contributor
alpapatel
Posts: 5
Registered: ‎11-14-2010
My Device: Not Specified
My Carrier: Software Engineer

Re: How can I add image to galery and view it from BlackBerry Media Center

I found solution to my question on this thread:

http://acodapella.blogspot.com/2010/05/reading-bbthumbsdat-thumbnail.html

 

Please use plain text.
Developer
robert30
Posts: 191
Registered: ‎04-02-2013
My Device: BlackBerry Bold 9900
My Carrier: developer

Re: How can I add image to galery and view it from BlackBerry Media Center

Hi alpapatel

Can you plz tell me the code for displaying image in thumbnail format i am also facing the problem and its urgent.

 

Thainks in advance

Please use plain text.