openRawResourceFd fails on android









up vote
5
down vote

favorite
2












I'm writing my first Android application, and I'm trying to read a res/raw resource file.



The following code throws a FileNotFound Exception:



AssetFileDescriptor fd = res.openRawResourceFd(R.raw.myfile);


but this line of code works:



InputStream stream = res.openRawResource (R.raw.myfile);


I need the AssetFileDescriptor in order to determine the length of the file. Any ideas why it isn't working?










share|improve this question























  • did you find any way to do this??
    – shridutt kothari
    Jun 9 '15 at 16:02














up vote
5
down vote

favorite
2












I'm writing my first Android application, and I'm trying to read a res/raw resource file.



The following code throws a FileNotFound Exception:



AssetFileDescriptor fd = res.openRawResourceFd(R.raw.myfile);


but this line of code works:



InputStream stream = res.openRawResource (R.raw.myfile);


I need the AssetFileDescriptor in order to determine the length of the file. Any ideas why it isn't working?










share|improve this question























  • did you find any way to do this??
    – shridutt kothari
    Jun 9 '15 at 16:02












up vote
5
down vote

favorite
2









up vote
5
down vote

favorite
2






2





I'm writing my first Android application, and I'm trying to read a res/raw resource file.



The following code throws a FileNotFound Exception:



AssetFileDescriptor fd = res.openRawResourceFd(R.raw.myfile);


but this line of code works:



InputStream stream = res.openRawResource (R.raw.myfile);


I need the AssetFileDescriptor in order to determine the length of the file. Any ideas why it isn't working?










share|improve this question















I'm writing my first Android application, and I'm trying to read a res/raw resource file.



The following code throws a FileNotFound Exception:



AssetFileDescriptor fd = res.openRawResourceFd(R.raw.myfile);


but this line of code works:



InputStream stream = res.openRawResource (R.raw.myfile);


I need the AssetFileDescriptor in order to determine the length of the file. Any ideas why it isn't working?







android file-descriptor android-assets






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '11 at 5:29









Vivek Kalkur

1,65521739




1,65521739










asked Nov 22 '11 at 4:54









WOPR

1422512




1422512











  • did you find any way to do this??
    – shridutt kothari
    Jun 9 '15 at 16:02
















  • did you find any way to do this??
    – shridutt kothari
    Jun 9 '15 at 16:02















did you find any way to do this??
– shridutt kothari
Jun 9 '15 at 16:02




did you find any way to do this??
– shridutt kothari
Jun 9 '15 at 16:02












3 Answers
3






active

oldest

votes

















up vote
5
down vote













You can do it this way:



FileDescriptor fd = getResources().openRawResourceFd(R.raw.rawResourceId).getFileDescriptor();


No try/catch block required.






share|improve this answer



























    up vote
    1
    down vote













    This works;



    AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);





    share|improve this answer



























      up vote
      0
      down vote













      Move your myfile to asset folder and try this



      try
      AssetFileDescriptor descriptor = getAssets().openFd( "myfile" );
      FileDescriptor fd = descriptor.getFileDescriptor();

      catch(Exception)





      Or you can try to open a RAW file from the resources/raw foder with this code:



      FileDescriptor fd = getResources().openRawResourceFd(R.raw.myfile).getFileDescriptor();





      share|improve this answer


















      • 1




        This is not the permanent solution, as in asset folder i am getting file is compressed error for which i need to put file in raw directory only see this thread stackoverflow.com/questions/6186866/…
        – shridutt kothari
        Jun 9 '15 at 16:03










      • openRawResource returns Inputstream and not FileInputStream
        – ozmank
        Feb 11 '16 at 8:42










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8222162%2fopenrawresourcefd-fails-on-android%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote













      You can do it this way:



      FileDescriptor fd = getResources().openRawResourceFd(R.raw.rawResourceId).getFileDescriptor();


      No try/catch block required.






      share|improve this answer
























        up vote
        5
        down vote













        You can do it this way:



        FileDescriptor fd = getResources().openRawResourceFd(R.raw.rawResourceId).getFileDescriptor();


        No try/catch block required.






        share|improve this answer






















          up vote
          5
          down vote










          up vote
          5
          down vote









          You can do it this way:



          FileDescriptor fd = getResources().openRawResourceFd(R.raw.rawResourceId).getFileDescriptor();


          No try/catch block required.






          share|improve this answer












          You can do it this way:



          FileDescriptor fd = getResources().openRawResourceFd(R.raw.rawResourceId).getFileDescriptor();


          No try/catch block required.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 '14 at 16:03









          Chepech

          3,52133460




          3,52133460






















              up vote
              1
              down vote













              This works;



              AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);





              share|improve this answer
























                up vote
                1
                down vote













                This works;



                AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  This works;



                  AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);





                  share|improve this answer












                  This works;



                  AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 '15 at 23:43









                  Jessicardo

                  6181612




                  6181612




















                      up vote
                      0
                      down vote













                      Move your myfile to asset folder and try this



                      try
                      AssetFileDescriptor descriptor = getAssets().openFd( "myfile" );
                      FileDescriptor fd = descriptor.getFileDescriptor();

                      catch(Exception)





                      Or you can try to open a RAW file from the resources/raw foder with this code:



                      FileDescriptor fd = getResources().openRawResourceFd(R.raw.myfile).getFileDescriptor();





                      share|improve this answer


















                      • 1




                        This is not the permanent solution, as in asset folder i am getting file is compressed error for which i need to put file in raw directory only see this thread stackoverflow.com/questions/6186866/…
                        – shridutt kothari
                        Jun 9 '15 at 16:03










                      • openRawResource returns Inputstream and not FileInputStream
                        – ozmank
                        Feb 11 '16 at 8:42














                      up vote
                      0
                      down vote













                      Move your myfile to asset folder and try this



                      try
                      AssetFileDescriptor descriptor = getAssets().openFd( "myfile" );
                      FileDescriptor fd = descriptor.getFileDescriptor();

                      catch(Exception)





                      Or you can try to open a RAW file from the resources/raw foder with this code:



                      FileDescriptor fd = getResources().openRawResourceFd(R.raw.myfile).getFileDescriptor();





                      share|improve this answer


















                      • 1




                        This is not the permanent solution, as in asset folder i am getting file is compressed error for which i need to put file in raw directory only see this thread stackoverflow.com/questions/6186866/…
                        – shridutt kothari
                        Jun 9 '15 at 16:03










                      • openRawResource returns Inputstream and not FileInputStream
                        – ozmank
                        Feb 11 '16 at 8:42












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Move your myfile to asset folder and try this



                      try
                      AssetFileDescriptor descriptor = getAssets().openFd( "myfile" );
                      FileDescriptor fd = descriptor.getFileDescriptor();

                      catch(Exception)





                      Or you can try to open a RAW file from the resources/raw foder with this code:



                      FileDescriptor fd = getResources().openRawResourceFd(R.raw.myfile).getFileDescriptor();





                      share|improve this answer














                      Move your myfile to asset folder and try this



                      try
                      AssetFileDescriptor descriptor = getAssets().openFd( "myfile" );
                      FileDescriptor fd = descriptor.getFileDescriptor();

                      catch(Exception)





                      Or you can try to open a RAW file from the resources/raw foder with this code:



                      FileDescriptor fd = getResources().openRawResourceFd(R.raw.myfile).getFileDescriptor();






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 10 at 12:51









                      Carlos Alberto Martínez Gadea

                      1,09311224




                      1,09311224










                      answered Nov 22 '11 at 5:32









                      Pawan

                      1,06021725




                      1,06021725







                      • 1




                        This is not the permanent solution, as in asset folder i am getting file is compressed error for which i need to put file in raw directory only see this thread stackoverflow.com/questions/6186866/…
                        – shridutt kothari
                        Jun 9 '15 at 16:03










                      • openRawResource returns Inputstream and not FileInputStream
                        – ozmank
                        Feb 11 '16 at 8:42












                      • 1




                        This is not the permanent solution, as in asset folder i am getting file is compressed error for which i need to put file in raw directory only see this thread stackoverflow.com/questions/6186866/…
                        – shridutt kothari
                        Jun 9 '15 at 16:03










                      • openRawResource returns Inputstream and not FileInputStream
                        – ozmank
                        Feb 11 '16 at 8:42







                      1




                      1




                      This is not the permanent solution, as in asset folder i am getting file is compressed error for which i need to put file in raw directory only see this thread stackoverflow.com/questions/6186866/…
                      – shridutt kothari
                      Jun 9 '15 at 16:03




                      This is not the permanent solution, as in asset folder i am getting file is compressed error for which i need to put file in raw directory only see this thread stackoverflow.com/questions/6186866/…
                      – shridutt kothari
                      Jun 9 '15 at 16:03












                      openRawResource returns Inputstream and not FileInputStream
                      – ozmank
                      Feb 11 '16 at 8:42




                      openRawResource returns Inputstream and not FileInputStream
                      – ozmank
                      Feb 11 '16 at 8:42

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8222162%2fopenrawresourcefd-fails-on-android%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands