[英]Throws file not found Exceptions
I created a gfx folder inside an android project's assets folder. 我在android项目的资产文件夹中创建了gfx文件夹。 I stored images which i will be using in my game for android.
我存储了将在我的Android游戏中使用的图像。 Since i need to pass the image height and image width converting it to the nearest highest powwer of 2 in andEngine, so i create a ImageUtility class to read image inside the gfx folder and get its height and width.
由于我需要将图像高度和图像宽度传递给andEngine,将其转换为最接近的2,因此我创建了ImageUtility类来读取gfx文件夹中的图像并获取其高度和宽度。
package org.ujjwal.androidGameUtility;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageUtilities {
private static final String ABSOLUTE_PATH = "F:\\Games\\TowerOfHanoi\\assets\\gfx\\";
private String fileName = "";
private File imageFile;
private int imageHeight;
private int imageWidth;
public ImageUtilities(String filename){
this.fileName = filename;
this.imageFile = new File(ABSOLUTE_PATH + this.fileName);
try {
processImage();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* this methods set the doucment relative path for the graphics to be used
* and it is mandotry to call before using ImageUtilties object else it will throw filenotFoundException
*
* @param path
*/
private void processImage() throws FileNotFoundException{
if(imageFile.exists()){
try {
BufferedImage image = ImageIO.read(this.imageFile);
this.imageWidth = image.getWidth();
this.imageHeight = image.getHeight();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else{
throw new FileNotFoundException("Either you missed typed filename or haven't called setAssetBasePathMethod setImageBasePath(String path) method");
}
}
public int getImageHeight(){
return this.imageHeight;
}
public int getImageWidth(){
return this.imageWidth;
}
public String getFileName(){
return this.fileName;
}
public File getImageFile(){
return this.imageFile;
}
}
I always get FileNotFoundException with the error msg above. 我总是得到FileNotFoundException与上面的错误味精。 The weird problem is when i access the image file from other java class i don't get any error.
奇怪的问题是,当我从其他Java类访问图像文件时,我没有得到任何错误。 I printed the height and width all went exactly i wanted but i could not access the same image file from my android game project.
我打印的高度和宽度都完全符合我的要求,但是我无法从我的Android游戏项目访问相同的图像文件。 What kind of error is this.
这是什么错误。 I provided absolute filepath for the image.
我提供了图像的绝对文件路径。 I also tried to compare the file path they were same.
我还尝试比较它们相同的文件路径。 Please tell me what error i got, I spend whole day trying to figure out but ...
请告诉我我遇到了什么错误,我整天都在努力弄清楚,但是...
What @mittmemo mentioned was right, you cannot access the F drive from your phone or emulator. @mittmemo提到的是正确的,您无法从手机或仿真器访问F驱动器。 Your computer and android are two different OS.
您的计算机和android是两个不同的操作系统。 What you can do is instead of putting your file in /assets, you can put it in /raw under resource directory.
您可以执行的操作是将文件放在资源目录下的/ raw中,而不是将文件放在/ assets中。 You can then access it by using:
然后,您可以使用以下方法访问它:
try {
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.yourfile);
byte[] b = new byte[in_s.available()];
in_s.read(b);
String str = new String(b);
} catch (Exception e) {
Log.e(LOG_TAG, "File Reading Error", e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.