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

Posted

技术标签:

【中文标题】如何在Android中引用原始文件夹中的文件【英文标题】:How to reference a File in raw folder in Android 【发布时间】:2012-04-02 21:22:15 【问题描述】:

我只想创建一个像这样的文件对象

File myImageFile = new File ("image1") ;

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

的例外情况

编辑: 其实我想做这样的事情

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

【问题讨论】:

你的图片文件夹在哪里? @djaqeel 抱歉 raw 文件夹不是 images 文件夹(位于 res 文件夹内).. 我也尝试访问 drawable 文件夹中的图像,但得到相同的错误 你真的需要文件吗,输入流对你有用吗? 【参考方案1】:

通常您通过 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,你可以在任何你需要的地方访问它。

【讨论】:

这对我帮助很大!谢谢!我需要该文件来为原始文件夹中保存的 Intent.EXTRA_STREAM 图像创建 Uri。【参考方案2】:

这里有 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();
    


从资产文件夹

/**
 * 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();


【讨论】:

关于“readRaw”方法,当它到达文件末尾时,测试变量变为空,因此它变得无用。 你能在不使用上下文的情况下以某种方式访问​​原始/资产吗?【参考方案3】:

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

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

【讨论】:

如果他知道文件名,我会说使用R.raw.id 是更好的选择。 @BorisStrandjev - 我同意,但有时人们更喜欢这样做。 我在很多地方都读过这是不可取的:更难编码并且性能更差。【参考方案4】:

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

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

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

【讨论】:

以上是关于如何在Android中引用原始文件夹中的文件的主要内容,如果未能解决你的问题,请参考以下文章

如何获取android 下的StreamingAssets文件夹中的xml文件

如何访问我放在 res 文件夹中的原始资源?

在 Android 中将资产或原始文件或资源文件作为 File 对象读取

如何在我的 Android 应用程序的原始文件夹中添加子文件夹? [复制]

如何获取原始文件夹uri路径并保存在android的内部存储器中?

如何引用公共目录(不是库)中的外部 jar 文件来使用 ant 构建 android 项目?