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
Contributor
Posts: 47
Registered: ‎08-26-2011
My Device: dsa
My Carrier: programmer
Accepted Solution

Is there any way to convert from Image to bitmap or anything else?

i want to save my Image to a file.
i know the path to save and get an Image . Anyone find a way to convert it to bitmap?

Developer
Posts: 112
Registered: ‎05-20-2011
My Device: 9800
My Carrier: Developer

Re: Is there any way to convert from Image to bitmap or anything else?

[ Edited ]

try this from your screen just pass the path to thos function

like getBitmap("file:///SDCard/img.png")

 

  private void getBitmap(String url)
    {       
        byte[] data = new byte[0];
        try
        {          
            FileConnection file = (FileConnection)Connector.open(url);
            int fileSize = (int)file.fileSize();
            data = new byte[fileSize];

               Bitmap img=Bitmap.createBitmapFromBytes(data,0,data.length,1);
            InputStream inputStream = file.openInputStream();           
            inputStream.read(data);
           
         
        }
        catch(Exception e)
        {
            System.out.println(e.toString());           
        }        
      
    }

 

 

if u like click on accept solution

Contributor
Posts: 47
Registered: ‎08-26-2011
My Device: dsa
My Carrier: programmer

Re: Is there any way to convert from Image to bitmap or anything else?

it can work if i get a path to the real image.
But i just get an Image type
public void saveToFile(Image image, String pathtosave){

 

so i need to convert from Image type to anything else what i can use to save my image to file.

i find the imag.getRGB() and Bitmap bm; bm.setARGB();
MAybe it is possible with them.
I giva a trySmiley Happy 

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

Re: Is there any way to convert from Image to bitmap or anything else?

Assuming you have Bitmap you are displaying, then use PNGEncodedImage or JPEGEncodedImage.  Convert the Bitmap to the form you want (PNG or JPEG and then use getData.

 

This assumes you have the image in a BB Bitmap.  You may be reading the raw bytes from somewhere else (like the network), in which case no 'conversion' is required, you can just write the bytes. 

Developer
Posts: 112
Registered: ‎05-20-2011
My Device: 9800
My Carrier: Developer

Re: Is there any way to convert from Image to bitmap or anything else?

[ Edited ]

try this

 

Image image2=Image.createImage("dfdd.png");
           int height=image2.getHeight();
                     int width=image2.getWidth();
                     int[] rgbdata = new int[width*height];
                     image2.getARGB(rgbdata, 0, width, 0, 0, width, height);
        byte[]    data1=BMPGenerator.encodeBMP(rgbdata, width, height);

Bitmap img=Bitmap.createBitmapFromBytes(data,0,data.length,1);

 

 

if u wnt BMPGenerator class i will send u

 

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

Re: Is there any way to convert from Image to bitmap or anything else?

[ Edited ]

@goving23

 

Thanks for trying to be helpful and supplying code.

 

However I think it would pay you to actually check your code before you post it.  Remember that people not familiar with BlackBerry will assume your code has been tested and working and will be confused when it doesn't work.  And your code doesn't work:

a) you return a byte array even though the return is set to void.

b) you convert the bytes to a Bitmap before you have read them!

 

As you can see, creating sample code is not as simple as cut/pasting.  You really need to be sure it works, which can take a lot of time.  This is one of the reasons I try to avoid giving people code.  I also think it is better to help people find out how to do it, and so help them learn, rather than write the code for them. 

 

 Edit:

Note that the code I am referring is in this Post:

http://supportforums.blackberry.com/t5/Java-Development/Is-there-any-way-to-convert-from-Image-to-bi...

 

Developer
Posts: 112
Registered: ‎05-20-2011
My Device: 9800
My Carrier: Developer

Re: Is there any way to convert from Image to bitmap or anything else?

sorry but i am now busy with project even i am trying to help people.

i think something is better than nothing.

all the developers can find this small issues. they can correct.

i think noone can use our code blindly.

they have to analize and then if it usefull then they will use.

anyway i will try to reduce my wrongs

Contributor
Posts: 47
Registered: ‎08-26-2011
My Device: dsa
My Carrier: programmer

Re: Is there any way to convert from Image to bitmap or anything else?

mage2.getARGB(rgbdata, 0, width, 0, 0, width, height);

isnt it overwrite width?
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Is there any way to convert from Image to bitmap or anything else?

Before we answer that, can you clarify

 

1) if you have an image file on

a) SD Card, or

b) resource in the application

 

2) If you wish to display this image in your BlackBerry application.

 

3) If you know what image format this image is in (png, jpg)

 

I think I may have misread your original question.

Contributor
Posts: 47
Registered: ‎08-26-2011
My Device: dsa
My Carrier: programmer

Re: Is there any way to convert from Image to bitmap or anything else?

[ Edited ]

//I'v got an image. I dontknow the format.. I'v got a type named WAImage it get an input stream in the construktor //and got a private Image::

private Image;

 

public WAImage(InputStream is){

 
Image nRes = null;
nRes = Image.createImage(is); 

this.Image=nRes;

}
 
//i want to do a methode named save to file this image to a place
//like:
public void saveToFile(String path){

...