尝试从Android Studio中的res->原始文件夹获取视频uri时找不到文件异常

时间:2017-10-09 07:00:43

标签: java android

我有以下代码从Android工作室的res->原始文件夹中获取视频(glass.avi)并保存到SD卡。但它显示文件未找到异常。

 vuri= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.glass);
        OutputStream out;

        try
        {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            FileInputStream fis = new FileInputStream(new File(String.valueOf(vuri)));

            byte[] buf = new byte[1024];
            int n;
            while (-1 != (n = fis.read(buf)))
                stream.write(buf, 0, n);

            byte[] bytes = stream.toByteArray();




        String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
        File createDir = new File(root+"master"+File.separator);
        createDir.mkdir();


        File file = new File(root + "master" + File.separator +"master.avi");

        file.createNewFile();
        out = new FileOutputStream(file);
        out.write(bytes);
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

它使用了以下代码。

getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
                             "raw", getPackageName());

将其作为InputStream

InputStream ins = getResources().openRawResource(
            getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
            "raw", getPackageName()));