简体   繁体   English

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

[英]File not found exception while trying to get the video uri from res->raw Folder in Android Studio

I have the below code to get the video(glass.avi) from res->raw folder in Android studio and save to sd card. 我有以下代码从Android Studio中的res-> raw文件夹中获取视频(glass.avi)并保存到SD卡。 But it is showing file not found exception. 但是它显示文件未找到异常。

 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();
    }

it worked with the below code. 它与下面的代码一起工作。

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

To get it as a InputStream 使其作为InputStream获得

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM