loading bitmaps

All your problems with Audio, Video and Images.

loading bitmaps

Postby tiswas » Tue Apr 06, 2010 2:44 pm

Hiya,

I'm very new to programming in general (so forgive what is probably a very obvious question, and try to be patient with your responses!), and am currently building a Java game which I am now trying to convert to android. One of the core classes in the game is an imagesLoader class, which reads a text file containing a list of file names, types, etc, then uses this to load bufferedImage. what I would like to do, is simply adapt this class so it will work with android. I need to load things to bitmaps, right? How is this done?

The way I would ideally like it to work would be:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String address = "imageDirectory/imageName.png"; //for example
  2.  
  3. Bitmap im = Bitmap(address);
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


i.e. a way of loading a bitmap when one has its address as a string.. Is this kind of thing possible? After this I'll need to adapt my draw methods (of course), but one thing at a time!

Thanks a lot,
tiswas
tiswas
Developer
Developer
 
Posts: 35
Joined: Thu Mar 11, 2010 1:33 pm

Top

Postby Luki » Wed Apr 07, 2010 1:17 pm

I'm not sure, but try this. It should work:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String address = "imageDirectory/imageName.png"; //for example
  3.  
  4. Bitmap im = BitmapFactory.decodeFile(address);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



Cheers, Lukas
Luki
Developer
Developer
 
Posts: 32
Joined: Fri Jul 24, 2009 12:59 pm

Postby tiswas » Wed Apr 07, 2010 10:50 pm

that looks great - thanks for helping out. Lots to sort before I can check if it works though!
tiswas
Developer
Developer
 
Posts: 35
Joined: Thu Mar 11, 2010 1:33 pm

Postby tiswas » Mon Apr 12, 2010 4:37 pm

hmm.. I can't get it to work.. My code is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 mSnowflake = BitmapFactory.decodeFile("/data/data/levy.adam.Images.player/playerStanding.png");
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


but I still get a force close.. Anyone have any ideas?
tiswas
Developer
Developer
 
Posts: 35
Joined: Thu Mar 11, 2010 1:33 pm

Postby Luki » Mon Apr 12, 2010 8:06 pm

Can you post the logcat output?
Luki
Developer
Developer
 
Posts: 32
Joined: Fri Jul 24, 2009 12:59 pm

Postby tiswas » Mon Apr 12, 2010 8:14 pm

Here are the errors I get (nothing is mentioned about not finding the image, I assume it just assigned null):

04-12 20:11:20.786: ERROR/AndroidRuntime(239): Uncaught handler: thread Thread-8 exiting due to uncaught exception
04-12 20:11:20.786: ERROR/AndroidRuntime(239): Uncaught handler: thread Thread-8 exiting due to uncaught exception
04-12 20:11:20.836: ERROR/AndroidRuntime(239): java.lang.NullPointerException
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at android.graphics.Canvas.drawBitmap(Canvas.java:980)
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at levy.adam.GameView$GameThread.doDraw(GameView.java:252)
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at levy.adam.GameView$GameThread.run(GameView.java:113)
04-12 20:11:20.836: ERROR/AndroidRuntime(239): java.lang.NullPointerException
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at android.graphics.Canvas.drawBitmap(Canvas.java:980)
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at levy.adam.GameView$GameThread.doDraw(GameView.java:252)
04-12 20:11:20.836: ERROR/AndroidRuntime(239): at levy.adam.GameView$GameThread.run(GameView.java:113)
tiswas
Developer
Developer
 
Posts: 35
Joined: Thu Mar 11, 2010 1:33 pm

Top

Postby tiswas » Mon Apr 12, 2010 8:16 pm

incidentally, I don't mind where I store my images, so long as I know what string to use to find them with the technique:

BitmapFactory.decodeFile(String address)
tiswas
Developer
Developer
 
Posts: 35
Joined: Thu Mar 11, 2010 1:33 pm

Postby nioupy » Mon Apr 12, 2010 8:37 pm

Hi,

I am not sure if that'll help you or mislead you [I am also a novice here], but I have been looking to load an image from memory to an ImageView, goign through a drawable. Using this code, I have been able to get my needs :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String address = "/data/data/myapp/img.png";
  3.  
  4. Drawable d = Drawable.createFromPath(address);
  5.  
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Checking this url http://www.androidsnippets.org/snippets/171/ , it seems you'll be able to use the drawable as a bitmap ... [might be too slow though]
nioupy
Developer
Developer
 
Posts: 36
Joined: Sat Apr 03, 2010 1:34 pm

Postby tiswas » Mon Apr 12, 2010 11:00 pm

that sounds good, but where in your eclipse project do you put the image? say your eclipse project is called myproj, and your package is called myapp. If your address is "/data/data/myapp/img.png" do you put the img in the myapp folder along side your source code, or just in the root myproj folder?
tiswas
Developer
Developer
 
Posts: 35
Joined: Thu Mar 11, 2010 1:33 pm

Postby tiswas » Tue Apr 13, 2010 12:28 am

hmm - actually I think my best bet is going to be to use AssetManager I think.. I've got the following code to work (with a string as the argument). yay!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. AssetManager assetManager = context.getAssets();
  2.  
  3.                        
  4.  
  5. Log.v(this.toString(), "loaded consequences...");
  6.  
  7.                        
  8.  
  9. mSnowflake = BitmapFactory.decodeStream(assetManager.open("snowflake.png"));
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
tiswas
Developer
Developer
 
Posts: 35
Joined: Thu Mar 11, 2010 1:33 pm

Top

Return to Multimedia Problems

Who is online

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