抛出找不到文件例外 [英] Throws file not found Exceptions

查看:144
本文介绍了抛出找不到文件例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Android项目的资产文件夹内的文件夹GFX。我保存,我将使用在我的游戏为Android的图像。因为我需要通过图像的高度和图像的宽度将其转换为在andEngine 2最近的最高powwer,所以我创建了一个ImageUtility类阅读GFX文件夹内的图像,并得到其高度和宽度。

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;
    }
}

我总是与上述错误味精得到FileNotFoundException异常。怪异的问题是,当我访问该图像文件与其他java类我没有得到任何错误。我打印的高度和宽度都去正是我想要的,但我不能从我的android游戏项目访问相同的图像文件。什么样的错误,这是。我的图像提供了绝对的文件路径。我也试图比较它们相同的文件路径。请告诉我什么错误我得到了,我花了一整天试图找出但是......

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 ...

推荐答案

是正确的,你不能从你的手机或模拟器访问F盘。电脑和Android是两个不同的操作系统。你可以做的是,而不是把你的文件/资产,你可以把它放在/原始资源目录下。然后,您可以通过访问:

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);
 }

这篇关于抛出找不到文件例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆