Howto load bitmaps ...

All your problems with Audio, Video and Images.

Howto load bitmaps ...

Postby misterbo » Mon Dec 31, 2007 4:31 pm

Hi everyone,

I am sure this must be very easy to achieve, however, I am somehow too stupid to do the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap photoBitmap = BitmapFactory.decode("/res/drawable/photo.jpg");
  2.  
  3.  
  4.  
  5. Canvas canvas = new Canvas(someBitmap);
  6.  
  7. Paint paint = new Paint();
  8.  
  9. canvas.drawBitmap(photoBitmap, 0, 0, paint);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


The idea is to draw an image file that is stored in the ressources to a canvas. Unfortunatelly, I keep on getting null pointer errors ...

I'd really appreciate if anyone could get me out here ...

Best,
misterbo
misterbo
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Dec 31, 2007 4:13 pm

Top

Postby problem » Mon Dec 31, 2007 6:10 pm

I guess one reason would be that:

Code: Select all
Bitmap photoBitmap = BitmapFactory.decode("/res/drawable/photo.jpg");


should really be

Code: Select all
Bitmap photoBitmap = BitmapFactory.decode( R.drawable.photo );
problem
Freshman
Freshman
 
Posts: 6
Joined: Mon Dec 31, 2007 6:07 pm
Location: Mesa, AZ

Postby misterbo » Mon Dec 31, 2007 6:20 pm

Thanks for your reply.

Unfortunatelly, this does not help: BitmapFactory.decode(String) expects the filepath as String and not an Integer.

Is there another function than decode to use? Respectively, what would be the correct absolute filepath if my image is stored in res/drawable?

Cheers,
misterbo
misterbo
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Dec 31, 2007 4:13 pm

Postby mago » Mon Dec 31, 2007 7:14 pm

The hint for the resource-id goes into the right direction.

You have to do something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap image = BitmapFactory.decodeResource(Resources.getSystem() , R.drawable.photo);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


But the first argument is wrong. I don't now what to use here.
The alternative is maybe the creation of a custom Resources-object (null don't does it).

Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)

ImageButton.setImageResource(int resid) is an example for the simple access by resource-id.

I think you can not access this image by its path. It's inside the dex-file. Aproaches like JarURLConnection don't work here. The way to go ist to reference it by its resource-id.
mago
Freshman
Freshman
 
Posts: 8
Joined: Thu Dec 27, 2007 5:56 pm
Location: Germany

Postby jeremian » Tue Jan 01, 2008 2:57 pm

This should work:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
  3.  
  4. canvas.drawBitmap(image, 0, 0, paint);
  5.  
  6.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And here comes the javadoc for the getResources() method:
Resources android.app.ApplicationContext.getResources()

public Resources getResources()
Return a Resources instance for your application's package.


Regards,
jeremian.
jeremian
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 13, 2007 11:28 pm

Postby misterbo » Tue Jan 01, 2008 3:34 pm

Sucess, this does the job!

Thank you very much, happy New Year everyone!

misterbo
misterbo
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Dec 31, 2007 4:13 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 10 guests