Loading a texture from assets folder for OpenGL - java

I know there are many questions relating to this question, but few appear to be for openGL.
I'm trying to load some PNG files from the assets folder into a Bitmap, but for some reason the returned Bitmap is null which in turn throws a NullPointerException here:
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
The code I am using to load the image from the assets folder:
public static Bitmap getBitmapFromAsset(AssetManager mgr, String path)
{
InputStream is = null;
Bitmap bitmap = null;
try {
is = mgr.open(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
bitmap = BitmapFactory.decodeStream(is, null, options);
}
catch (final IOException e)
{
bitmap = null;
Log.e(TAG, "FAILED TO get getBitmapFromAsset: " + e.getMessage());
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException ignored)
{
}
}
}
return bitmap;
}
I've tried it a few different ways, e.g without the BitmapFactory.Options, but no matter what I am getting the NullPointerException, so I'm guessing there's another procedure I should be doing.
P.S. I can load them from the res/raw folder, but I can't have subdirectories to organise my assets.

OK, I just realised that my assets folder was under the res folder for some reason. I just put it under the main folder app/main/assets and it's working.
*blushes

Related

Read and write bitmap to android fails

I am trying to write and read a bitmap following the suggestions on other topics about this, the thing is i never get the bitmap when i try to read on the path where i saved the image:
So i have this to write the bitmap:
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"captured");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
}
i pass the returned path to another activity and then i pass it as parameter to get the bitmap like this:
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "captured.jpg");
Log.d("filehe",f.toString());
b = BitmapFactory.decodeStream(new FileInputStream(f));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
i feel i am doing something wrong here, but can't figure out what, the b variavel has no value :/.
Any help?
Thanks
So i have this to write the bitmap:
The file that you save is named captured.
i pass the returned path to another activity and then i pass it as parameter to get the bitmap like this
Here, you are trying to load captured.jpg, which is not captured.
You could avoid this sort of problem by having the first method return the File that the second method then uses.
Also:
Use an image-loading library (e.g., Picasso, Glide) that has an in-memory cache, so you do not waste the user's time re-loading the same bitmap from disk
Get rid of ContextWrapper from the first method, as you do not need it

Loading images in java (eclipse vs IntelliJ)

One would load an image:
project>res>img.png (path = "res/img.png")
BufferedImage image = loadImage(path);
Where LoadImage is:
protected BufferedImage loadImage(String path) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(path));
} catch(IOException e) {
System.err.println("could not load: " + path);
}
return img;
}
Someone using Eclipse used:
(path = "/img.png")
BufferedImage image = null;
try {
image = ImageIO.read(Sprite.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}
But using this in IntelliJ gives:
Exception in thread "Game_main" java.lang.IllegalArgumentException:
input == null!
why getResourceAsStream fails?
getResourceAsStream() uses (by default) the system classloader to find the file. Therefore, the resources directory has to be on the classpath - check that the IntelliJ project is correctly including the res directory and marking it as a resource directory.

Loading a BufferedImage in a .jar

I'm currently trying working on an own game and created a Animation class, my problem is that i want the programm to be able to still find all the images when i create a jar out of it so I tried to load an Image via
BufferedImage img = ImageIO.read(getClass().getClassLoader().getResourceAsStream("player.png"));
but when I start the code I get a NullPointerException, i checked the location twice but the image exists and there should be no problems, can anyone help me out a bit?
try this
public BufferedImage loadImage(String fileName){
BufferedImage buff = null;
try {
buff = ImageIO.read(getClass().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
return null;
}
return buff;
}

Failed to show Default Images when passed imagename is not found in assets in Android?

Here is my code i have written to get images from assets folder by passing imagename from database.But when passedImage name is not found in assets folder then i want to show some dummy images my code is throwing only catch block when no image is found but its not showing default image and no log is print in case of image not found .....
private Bitmap getBitmapFromAsset(String strName) {
AssetManager assetManager = getAssets();
InputStream istr = null;
Bitmap bitmap = null;
try {
istr = assetManager.open(strName);
if(istr.equals("null"))
{
Log.i("getBitmapFromAsset isStr",""+istr);
bitmap = BitmapFactory.decodeStream(assetManager.open("save_fatwa.jpg"));
}
else
{
bitmap = BitmapFactory.decodeStream(istr);
}
} catch (Exception e) {
Log.i("getBitmapFromAsset",""+bitmap);
e.printStackTrace();
}
return bitmap;
}
AssetManager.open returns an InputStream, not a string. If should be if(istr==null) not if it equals the string "null".

Why Image file not found in android on server but open in browser

I am trying to download image from server. Few are downloading and few and creating problem. I don't know why.
I have downloaded and show image to user on the same location. Here is the file which is able to download.
http://www.mongreldog.co.nz/unilogo/Backgrounds_20399.png
When I am trying to download following image. This image is opening in browser but not downloading in android
http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester%20United_M_785.jpg
Its give exception
java.io.FileNotFoundException: http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester United_M_785.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
at src.com.mongreldog.appsupport.Utils.downloadImage(Utils.java:77)
at src.com.mongreldog.ViewFullCompAndCommentActivity$3.performInBackground(ViewFullCompAndCommentActivity.java:607)
at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:44)
at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
Here is my code.
public static Bitmap downloadImage(String imageURLStr) {
Bitmap bitmap = null;
InputStream in = null;
try {
URL url = new URL(imageURLStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (SocketTimeoutException e) {
bitmap = null;
e.printStackTrace();
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
bitmap = null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
bitmap = null;
}
return bitmap;
}
Use URL Encoder to encode the image URL, as you can see the URL have white spaces in the log report.
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = URLEncoder.encode(imageURLStr, "utf-8");
//... rest of your code.
}
Edit: as you reported of issue '+' instead of %20
you can use
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = imageURLStr.replaceAll(" ", "%20");
//... rest of your code.
}
For source check here
I try to use URLEncoder.encode() to encode the URL. Its strange that it convert " " with "+".
Please try Uri.encode(imageURL). I just try it and its working perfectly.
I have tested that in android.
Looks like the file name is not being parsed correctly (the space between 'Manchester' and 'United').
Use URLEncoder.encode() to encode the URL.
Your second URL is very instable. It may return 404 in the most cases even in Chome. I have seen the picture only once.

Categories