简体   繁体   中英

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. 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 ins = getResources().openRawResource(
            getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
            "raw", getPackageName()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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