BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/index.php)
-   Developer Forum (http://www.blackberryforums.com/forumdisplay.php?f=15)
-   -   Displaying Sample/User Pictures in Application (http://www.blackberryforums.com/showthread.php?t=83082)

Californium 06-26-2007 12:22 PM

Displaying Sample/User Pictures in Application
 
Greetings,


I am trying to load a picture from either the preloaded sample picture folder or the user picture folder and display it within my application running on OS4.2.x. I am able to extract the URI through a FileConnection and searching at "file:///store/samples/pictures/" and "file:///store/home/user/pictures". However, I am unable to find a way to display the picture. Once I have obtained the file path, is there a way to convert it to a Bitmap or Image for displaying? Any alternative methods?


Thank you,
Californium

egalexe 06-26-2007 12:40 PM

Bitmap myBitmap = Bitmap.getBitmapResource("ProjRoot/Path");
?

Californium 06-26-2007 12:42 PM

That seems to give an error:

FRIDG: could not find file:///store/samples/pictures/BBTiles-Orange.png

Rose 06-27-2007 12:03 AM

Hi ,
file:///store/samples/pictures/ is used to read files that are stored in SDCard or device memory..

Bitmap.getBitmapResource("ProjRoot/Path"); this is used to read a file from the jar. To use this code you need to package the image with in the jar.

To display image from the internal memory read the file as bytes and then create
byte[] data;
EncodedImage.createEncodedImage(data,0,data.length );


Then use can draw the encoded image to a bitmapField or you can create a bitmap from the encoded image.

Californium 06-27-2007 09:59 AM

Thank you for the information. I will try that out!


Regards,
Californium


Note: If anyone is curious, here's the code to display the image.
Code:

/*
 * PictureViewer.java
 *
 * © <your company here>, 2003-2005
 * Confidential and proprietary.
 */

package pictureviewer;

import java.io.*;
import java.lang.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.*;


/**
 *
 */
class PictureViewer extends UiApplication{
   
    public static void main(String[] args)
    {
        //create a new instance of the application
        //and start the application on the event thread
        PictureViewer theApp = new PictureViewer();
        theApp.enterEventDispatcher();
    }
   
    PictureViewer() { 
        MainScreen main = new MainScreen();

        try {
            FileConnection fconn = (FileConnection)Connector.open("file:///store/samples/pictures/DuskClouds.jpg");
            // If no exception is thrown, then the URI is valid, but the file may or may not exist.
            if (fconn.exists()) {
                InputStream input = fconn.openInputStream();
                int available = input.available();
                byte[] data = new byte[available];
                input.read(data, 0, available);
                EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);
                Bitmap b = image.getBitmap();
                BitmapField picture = new BitmapField(b);
                main.add(picture);
                main.add(new LabelField("Data Length:" + data.length));
            }
            else {
                main.add(new LabelField("Picture does not exist"));
            }
            fconn.close();
        }
        catch (Exception ioe) {
            main.add(new LabelField("Error"));
        }
        pushScreen(main);
    }
}


Pook 09-17-2007 11:44 AM

Is there any version 4.0 alternative code for the above because package

Code:

javax.microedition.io.file.*
is missing and hence no FileConnection.

banu_berry 10-19-2007 03:40 AM

is there any method to display a txt file(text.txt) once u have obtained the file path..

would appreciate for response thanks in advance

Smiley8 06-24-2008 05:48 PM

I have Pearl 8100 with OS 4.2.1.109 and the code wont run. It returns Error. do you know why?

Ivanov 06-27-2008 09:46 AM

It seems that the access to the "embedded" image files is completely disallowed...
I had the same problem when I needed an image file for testing the code and was too lazy to copy my own to the device. However the code worked with "normal" images but not with those provided with the device

mickymouse 09-22-2008 11:05 AM

Is their is a way to get the h t t p:// instead a file:// I need help . Please reply

goulamass 10-01-2008 08:22 AM

Hum I try this way but my program throw an error when :

Bitmap b = image.getBitmap();

I know that the path is correct and my byteArray too because I'm successful to send it to a web service.

What's wrong??

I use the code given excatly

goulamass 10-02-2008 11:11 AM

Here there is my code

Code:

FileConnection fconn = (FileConnection)Connector.open(cheminPhoto1);
               
System.out.println("Connexion établie");
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (fconn.exists())
{

                    InputStream input = fconn.openInputStream();

                    int available = input.available();

                    byteArray = new byte[available];

                    input.read(byteArray, 0, available);

                    //I know that the pb comes from this line but I don't know why
                    EncodedImage image = EncodedImage.createEncodedImage(byteArray,0,byteArray.length);

                    Bitmap b = image.getBitmap();

                    BitmapField picture = new BitmapField(b);

                    add(picture);
}

Is someone can tell me what's wrong??

I know that my byteArray is correct because I send it to a web service and this last recover the correct picture

goulamass 10-03-2008 02:45 AM

No one can tell me what's wrong???

simon.hain 10-06-2008 02:23 AM

not sure if it applies but at least pictures stored with a contact are encoded in base64.

Code:

byte[] photoEncoded = blackberryContact.getBinary(BlackBerryContact.PHOTO, 0);
byte[] photoDecoded = Base64InputStream.decode(photoEncoded, 0, photoEncoded.length);


goulamass 10-06-2008 02:43 AM

But it's not a picture stored with a contact.

The picture comes from the camera.

goulamass 10-06-2008 09:58 AM

Hum I found a solution.

the problem seems to come from

byteArray = new byte[available];
input.read(byteArray, 0, available);

I don't know what but it doesn't wrok my byte Array is allways null.

So I do this :

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int j = 0;
while((j=input.read())!=-1)
{
baos.write(j);
}
byteArray = baos.toByteArray();

I know that is not a very fast an elegant operation but it works


All times are GMT -5. The time now is 04:42 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.