Throws file not found Exceptions

旧城冷巷雨未停 提交于 2020-01-06 22:45:57

问题


I created a gfx folder inside an android project's assets folder. I stored images which i will be using in my game for 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.

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. The weird problem is when i access the image file from other java class i don't get any error. 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. 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 ...


回答1:


What @mittmemo mentioned was right, you cannot access the F drive from your phone or emulator. Your computer and android are two different OS. What you can do is instead of putting your file in /assets, you can put it in /raw under resource directory. 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);
 }


来源:https://stackoverflow.com/questions/31079930/throws-file-not-found-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!