So I have a script (its a ModPE) and I want to load an image. My question is, how do I load an image using the Bitmapfactory.decodeFile()
method which is in the same directory as the script?
Asked
Active
Viewed 100 times
-1

ButchMonkey
- 1,873
- 18
- 30

PG Cubing
- 11
- 5
-
Why did you tag this both `java` and `javascript`? They're different languages. – Joseph Sible-Reinstate Monica Jan 23 '20 at 20:46
-
Because it's a ModPE which is basically Java in a .js file ik it's weird – PG Cubing Jan 23 '20 at 21:07
1 Answers
0
You can use ./
to define current working directory (CWD)
String myImage = "./myFileInCWD.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(myImage);
The .
is important as /
means the root or top level directory

ButchMonkey
- 1,873
- 18
- 30
-
I tried that, but it doesn't load so I log it, and it's just an empty object. Is there a problem with the image if that happens? – PG Cubing Jan 23 '20 at 19:31
-
You can use this to get the path that the app was initialised in and check that it is the path you are expecting. [`Path path = FileSystems.getDefault().getPath(".")`](https://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java) Sometimes the physical location of a file is not where it is ran from. You can then use the same function to get the path of your file `Path path = FileSystems.getDefault().getPath(myFile);` – ButchMonkey Jan 23 '20 at 19:40
-
The thing is, this is a ModPE, which is a mod for Minecraft pocket edition and it's in a .js file, so I can't import things properly and it's difficult to use normal android things... So the path probably won't work. – PG Cubing Jan 23 '20 at 21:06
-
1Run the command I posted above to figure out which directory the script runs in. Otherwise, I suggest you make a new issue that details everything you have tried and what you have mentioned in the comments because clearly its more than just "how to load an image that is in the same directory" – ButchMonkey Jan 23 '20 at 22:22