Jack
Jack

Reputation: 151

Create Bitmap from jpeg

In android, I'm trying to create a bitmap from a jpeg that I downloaded and copied to a drawable folder under res.

attached is my code:

public void draw(Canvas g, Resources res, int x, int y, int w, int h) {
    Bitmap im = BitmapFactory.decodeResource(res, R.drawable.green_dragon);
    Bitmap im = BitmapFactory.decodeFile(R.drawable.green_dragon);
    g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new Paint());
}

Android does not recognize the R.drawable.green_dragon in either the decodeResource or decodeFile lines. I have also tried both refreshing and cleaning the application. Neither helped. When I looked up the image properties the type is File and the path is .jpg.

Thanks in advance for your assistance.

Upvotes: 1

Views: 10380

Answers (2)

dmaxi
dmaxi

Reputation: 3297

Maybe the srcRect for drawBitmap should not be null but this:

Rect srcRect = new Rect(0, 0, im.getWidth(), im.getHeight());

Upvotes: 0

slayton
slayton

Reputation: 20319

Only resources that are packaged with the application can be referenced using the R object.

Anything you download must either be saved to a database or a file. I'm assuming you've already downloaded and saved the file. At which point you either need the a String that has the Path of the jpg or a FileDescriptor for the jpg.

Once you have either you can load the bitmap using:

Bitmap bmp = BitmapFactory.decodeFile( PathToFileString ); 

or

Bitmap bmp = BitmapFactory.decodeFileDescriptor( fileDescriptorObject );

Upvotes: 3

Related Questions