[英]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.