Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java – 如何在Android中引用原始文件夹中的文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我只想创建一个这样的File对象

File myImageFile = new File(“image1”);

但它给我的FileNotFoundException异常
我如何在原始文件夹中引用一个文件

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

multipartentity multipartentity = new multipartentity(httpR_917_11845@ultipartMode.bROWSER_COMPATIBLE);
multipartentity.addPart(“uploaded”,new FileBody(new File(“myimage”)));

解决方法

这里有2个功能.一个从RAW读取,一个从资产中读取
/**
 * 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,len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException E) {
        }
    } catch (IOException E) {
    }
    return outputStream.toString();

}

大佬总结

以上是大佬教程为你收集整理的java – 如何在Android中引用原始文件夹中的文件全部内容,希望文章能够帮你解决java – 如何在Android中引用原始文件夹中的文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。