2

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();
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
djac
  • 187
  • 19

1 Answers1

0

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()));
djac
  • 187
  • 19
  • 1
    Hello, I am getting the same error while getting sound file for push notification from raw folder. How did you converted this InputStream to a uri? – Megha Maniar Jan 12 '19 at 07:24