如何在Android中的原始文件夹中引用文件

时间:2012-03-17 18:01:12

标签: java android file filenotfoundexception

我只想创建一个像这样的

文件对象

File myImageFile = new File ("image1") ;

但它给了我 FileNotFoundException 的例外 如何引用原始文件夹中的文件

修改 其实我想做这样的事情

MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multipartEntity.addPart("uploaded", new FileBody(new File("myimage")));

4 个答案:

答案 0 :(得分:7)

通常,您通过getResources()。openRawResource(R.id._your_id)访问文件。如果您绝对需要对其进行文件引用,则可以选择将其复制到内部存储:

File file = new File(this.getFilesDir() + File.separator + "DefaultProperties.xml");
try {
        InputStream inputStream = resources.openRawResource(R.id._your_id);
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        byte buf[]=new byte[1024];
        int len;
        while((len=inputStream.read(buf))>0) {
            fileOutputStream.write(buf,0,len);
        }

        fileOutputStream.close();
        inputStream.close();
    } catch (IOException e1) {}

现在您可以在任何需要的地方访问File

答案 1 :(得分:3)

这里有2个功能。一个从RAW读取,另一个从Assets读取

/**
 * Method to read in a text file placed in the res/raw directory of the
 * application. The method reads in all lines of the file sequentially.
 */

public static void readRaw(Context ctx,int res_id) {

    InputStream is = ctx.getResources().openRawResource(res_id);
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
                                                        // size

    // More efficient (less readable) implementation of above is the
    // composite expression
    /*
     * BufferedReader br = new BufferedReader(new InputStreamReader(
     * this.getResources().openRawResource(R.raw.textfile)), 8192);
     */

    try {
        String test;
        while (true) {
            test = br.readLine();
            // readLine() returns null if no more lines in the file
            if (test == null)
                break;
        }
        isr.close();
        is.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

并从Assets文件夹

/**
 * Read a file from assets
 * 
 * @return the string from assets
 */

public static String getQuestions(Context ctx,String file_name) {

    AssetManager assetManager = ctx.getAssets();
    ByteArrayOutputStream outputStream = null;
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open(file_name);
        outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
        }
    } catch (IOException e) {
    }
    return outputStream.toString();

}

答案 2 :(得分:1)

您可以将其打开为InputStream,我不知道是否可以作为文件:

int rid = resources.getIdentifier(packageName + ":raw/" + fileName, null, null);  
//get the file as a stream  
InputStrea is = resources.openRawResource(rid);  

答案 3 :(得分:1)

您可以使用 InputStreamBody 代替 FileBody ,以便您可以像这样使用它:

InputStream inputStream = resources.openRawResource(R.raw.yourresource);

MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("uploaded", new InputStreamBody(inputStream));