如何在android中获取原始文件或原始目录?

时间:2015-10-18 20:29:19

标签: android file resourcedictionary

我想尝试oobjloader。 (自由波3d格式加载器)我加载了第一个.obj,但没有找到原始目录。 (或者只是没找到文件)

            Uri url = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.cube1);
            String filename = url.toString();

            try {
                Build builder = new Build();
                Parse obj = new Parse(builder, filename);
            } catch (java.io.FileNotFoundException e) {
                System.out.println("trace FileNotFoundException loading file " + filename + ", e=" + e);
                e.printStackTrace();
            } catch (java.io.IOException e) {
                System.out.println("trace IOException loading file " + filename + ", e=" + e);
                e.printStackTrace();
            }

如何获取原始文件dir url?

错误:

    10-18 22:10:42.714    6136-6136/com.solar.asc.solar I/System.out﹕ trace FileNotFoundException loading file android.resource://com.solar.asc.solar/2131099648, e=java.io.FileNotFoundException: /android.resource:/com.solar.asc.solar/2131099648: open failed: ENOENT (No such file or directory)
    10-18 22:10:42.714    6136-6136/com.solar.asc.solar W/System.err﹕ java.io.FileNotFoundException: /android.resource:/com.solar.asc.solar/2131099648: open failed: ENOENT (No such file or directory)
    10-18 22:10:42.714    6136-6136/com.solar.asc.solar W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:416)
    10-18 22:10:42.714    6136-6136/com.solar.asc.solar W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)

我也试过其他格式,但没有帮助。

"android.resource://" + getPackageName() + "/raw/cube1"
"android.resource://" + getPackageName() + "/raw/cube1.obj"

Thx all answare。

1 个答案:

答案 0 :(得分:0)

If wanna use the cube1.obj file from raw, you can do it this way

        InputStream in = null;
        OutputStream out = null;
        File outFile = null;
        try 
        {
            in = context.getResources().openRawResource(R.raw.cube1);
            outFile = new File(<path where you are going to store this raw file>, <what will be name of file> );
            out = new FileOutputStream(outFile);
            byte[] buffer = new byte[BBConstants.KILO_VALUE];
            int read;
            while((read = in.read(buffer)) != -1)
            {
                out.write(buffer, 0, read);
            }
        }
    } 
    catch(IOException e) 
    {

    } 

现在outFile将包含你的cube1.obj数据,你可以在你想要使用的地方使用它。

相关问题