[英]Throws file not found Exceptions
我在android项目的资产文件夹中创建了gfx文件夹。 我存储了将在我的Android游戏中使用的图像。 由于我需要将图像高度和图像宽度传递给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;
}
}
我总是得到FileNotFoundException与上面的错误味精。 奇怪的问题是,当我从其他Java类访问图像文件时,我没有得到任何错误。 我打印的高度和宽度都完全符合我的要求,但是我无法从我的Android游戏项目访问相同的图像文件。 这是什么错误。 我提供了图像的绝对文件路径。 我还尝试比较它们相同的文件路径。 请告诉我我遇到了什么错误,我整天都在努力弄清楚,但是...
@mittmemo提到的是正确的,您无法从手机或仿真器访问F驱动器。 您的计算机和android是两个不同的操作系统。 您可以执行的操作是将文件放在资源目录下的/ raw中,而不是将文件放在/ assets中。 然后,您可以使用以下方法访问它:
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.