ImageIO can't read image input file - java

This is my first post. Excuse me if I'm making an obvious mistake.
BufferedImage img = null;
File file = new File("com/game/assets/badlogic.jpg");
try {
img = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
This is my code and it can't read my image input file for some reason.
This is the error message:
javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
at com.engine.Main.run(Main.java:41)
at java.base/java.lang.Thread.run(Thread.java:844)
at com.engine.Main.start(Main.java:32)
at com.game.GameManager.main(GameManager.java:51)
I have no clue what am I doing wrong. Anyways Thank You, kind Strangers.
I got it to work!
BufferedImage img = null;
try {
img = ImageIO.read(new FileInputStream("/Users/earmalys/Desktop/InteliJ projects/mantengine/src/com/game/assets/badlogic.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
But the file path is very exact. Is there anyway I can get around this?

Whenever I get a BufferedImage from a file, I find it much easier to use a FileInputStream rather than a File. Here's an example:
try {
BufferedImage image = ImageIO.read(new FileInputStream("com/game/assets/badlogic.jpg"));
}
catch (IOException e) {
e.printStackTrace();
}

Related

How to load the image from a jar file?

This code works only if I run my project, but if I run the Jar file, I get a NullPointerException. The image is in src directory.
try {
URL resource = Sticker.class.getResource("\\transparentSticker.png");
img = null;
this.img = ImageIO.read(Paths.get(resource.toURI()).toFile());
System.out.println("c");
} catch (IOException | URISyntaxException e) {
System.out.println("caught");
}
If I use this code, an IllegalArgumentException is being thrown.
InputStream input = Sticker.class.getResourceAsStream("\\transparentSticker.png");
if (input == null) {
input = Sticker.class.getClassLoader().getResourceAsStream("\\transparentSticker.png");
}
try {
img = ImageIO.read(input);
} catch (IOException e) {
e.printStackTrace();
}
How to load the image?
You did say the PNG is in the src folder?
Let's see, after reading this and this, I think it needs to be:
InputStream input = Sticker.class.getResourceAsStream("/src/transparentSticker.png");
or
URL resource = Sticker.class.getResource("/src/transparentSticker.png");
Think unix, not windows.

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

Reading an image resource file in java

I've been searching to find out how to solve this problem for some time now, but I can't seem to find a way. I could read the image as long as it was in eclipse, but when I exported it, it was impossible. I tried with an InputStream, but that throws an IllegalArgumentException for some reason, what am I doing wrong?
InputStream iconstream = getClass().getResourceAsStream("resources/icon.png");
InputStream pigstream = getClass().getResourceAsStream("resources/pig.png");
This is for getting the resources, and here is where I read them:
try {
icon = ImageIO.read(iconstream);
pig = ImageIO.read(pigstream);
} catch(IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
} catch(IllegalArgumentException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
change
InputStream iconstream = getClass().getResourceAsStream("resources/icon.png");
to
InputStream iconstream = getClass().getResourceAsStream("/resources/icon.png");
getClass().getResourceAsStream() looks in the package of the class. You either need to add a leading / to the name or else use getClass().getClassLoader().getResourceAsStream().

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;
}

Java Android working with InputStream and File

Well, first of all, I'm just learning and don't quite understand what I'm doing.
What I want is to create an Excel file in memory and then it would be possible to send it with ActionBarSherlock's ShareActionProvider to mail for example.
But I got exeption :
11-24 18:45:52.112: W/System.err(22073): java.io.FileNotFoundException: /Competition.xls: open failed: EROFS (Read-only file system)
As I searched for the answer on the web - it's the problem of file being created in the system area which is read-only. But I want to create it in memory.. Somehow. Once again, I don't really understand well how it works - the way I see it - I create .xls file somewhere in the memory. So the explanation would be helpful.
So, here's the code :
private void createFileTosend() {
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
File toSend=null;
try {
toSend = getFile();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inputStream = new BufferedInputStream(new FileInputStream(toSend));
outputStream = openFileOutput("Competition.xls",
Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = inputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, length);
}
} catch (IOException ioe) {
/* ignore */
}
} catch (FileNotFoundException fnfe) {
/* ignore */
} finally {
try {
inputStream.close();
} catch (IOException ioe) {
/* ignore */
}
try {
outputStream.close();
} catch (IOException ioe) {
/* ignore */
}
}
}
public File getFile() throws IOException, WriteException{
File file=new File("Competition.xls");
WritableWorkbook workbook = Workbook.createWorkbook(file);
//then goes creation of Excel 's xls file which is not important for the question
workbook.write();
workbook.close();
return file;
}
Once again, don't downvote me, please, I'm just learning
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Ass per the error log you post, i guess you didn't read it carefully. IT is saying read only file system. You need to put the above permission ATLEAST in your android manifest.
Do this much and see if there is any more error or not
I have a doubt may be wrong
outputStream = openFileOutput("Competition.xls",
Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
you are using Context.MODE_WORLD_READABLE its readable, so how can you access it in write mode later. Is it right?
Check this link
Use the http://developer.android.com/guide/topics/data/data-storage.html to clear your doubt about file creation

Categories