Android FileInputStream exception file not found












0















I'm trying to untar a tar with jtar and I'm getting




java.io.FileNotFoundException: Download/Data.tar (No such file or directory)




I'm not using an emulator, it's directly on my phone.



The file does exist in my android's downloads folder (even before the app starts).



I have all the permissions to read/write.



File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
File destFolder = new File(Environment.DIRECTORY_DOCUMENTS + "/My Data/");
if(!destFolder.exists())
{
// Make it, if it doesn't exit
destFolder.mkdirs();
}else{
//TODO
//delete the folder contents
}


// Create a TarInputStream
try {
TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
TarEntry entry;

while ((entry = tis.getNextEntry()) != null) {
int count;
byte data = new byte[2048];
FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
BufferedOutputStream dest = new BufferedOutputStream(fos);

while ((count = tis.read(data)) != -1) {
dest.write(data, 0, count);
}

dest.flush();
dest.close();
}

tis.close();
}catch (Exception e){
Toast toast2 = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
toast2.show();
Log.e("TAG", "error", e);
}


Logcat says the error is on the first line after the try



EDIT: for some reason it doesn't even create the /My Data/ folder even though it does go to destFolder.mkdirs(); when I'm debugging. not sure how it relates



EDIT 2: I discovered that the tarFile.exists() and tarFile.canRead() says false, I tried to put an explicit path "storage/emulated/0/Download/Data.tar" AND IT WORKS, it exists and readable.



Is there any danger leaving it like this? would some phones have a different ...emullated/X/... number?










share|improve this question





























    0















    I'm trying to untar a tar with jtar and I'm getting




    java.io.FileNotFoundException: Download/Data.tar (No such file or directory)




    I'm not using an emulator, it's directly on my phone.



    The file does exist in my android's downloads folder (even before the app starts).



    I have all the permissions to read/write.



    File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
    File destFolder = new File(Environment.DIRECTORY_DOCUMENTS + "/My Data/");
    if(!destFolder.exists())
    {
    // Make it, if it doesn't exit
    destFolder.mkdirs();
    }else{
    //TODO
    //delete the folder contents
    }


    // Create a TarInputStream
    try {
    TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
    TarEntry entry;

    while ((entry = tis.getNextEntry()) != null) {
    int count;
    byte data = new byte[2048];
    FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
    BufferedOutputStream dest = new BufferedOutputStream(fos);

    while ((count = tis.read(data)) != -1) {
    dest.write(data, 0, count);
    }

    dest.flush();
    dest.close();
    }

    tis.close();
    }catch (Exception e){
    Toast toast2 = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
    toast2.show();
    Log.e("TAG", "error", e);
    }


    Logcat says the error is on the first line after the try



    EDIT: for some reason it doesn't even create the /My Data/ folder even though it does go to destFolder.mkdirs(); when I'm debugging. not sure how it relates



    EDIT 2: I discovered that the tarFile.exists() and tarFile.canRead() says false, I tried to put an explicit path "storage/emulated/0/Download/Data.tar" AND IT WORKS, it exists and readable.



    Is there any danger leaving it like this? would some phones have a different ...emullated/X/... number?










    share|improve this question



























      0












      0








      0








      I'm trying to untar a tar with jtar and I'm getting




      java.io.FileNotFoundException: Download/Data.tar (No such file or directory)




      I'm not using an emulator, it's directly on my phone.



      The file does exist in my android's downloads folder (even before the app starts).



      I have all the permissions to read/write.



      File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
      File destFolder = new File(Environment.DIRECTORY_DOCUMENTS + "/My Data/");
      if(!destFolder.exists())
      {
      // Make it, if it doesn't exit
      destFolder.mkdirs();
      }else{
      //TODO
      //delete the folder contents
      }


      // Create a TarInputStream
      try {
      TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
      TarEntry entry;

      while ((entry = tis.getNextEntry()) != null) {
      int count;
      byte data = new byte[2048];
      FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
      BufferedOutputStream dest = new BufferedOutputStream(fos);

      while ((count = tis.read(data)) != -1) {
      dest.write(data, 0, count);
      }

      dest.flush();
      dest.close();
      }

      tis.close();
      }catch (Exception e){
      Toast toast2 = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
      toast2.show();
      Log.e("TAG", "error", e);
      }


      Logcat says the error is on the first line after the try



      EDIT: for some reason it doesn't even create the /My Data/ folder even though it does go to destFolder.mkdirs(); when I'm debugging. not sure how it relates



      EDIT 2: I discovered that the tarFile.exists() and tarFile.canRead() says false, I tried to put an explicit path "storage/emulated/0/Download/Data.tar" AND IT WORKS, it exists and readable.



      Is there any danger leaving it like this? would some phones have a different ...emullated/X/... number?










      share|improve this question
















      I'm trying to untar a tar with jtar and I'm getting




      java.io.FileNotFoundException: Download/Data.tar (No such file or directory)




      I'm not using an emulator, it's directly on my phone.



      The file does exist in my android's downloads folder (even before the app starts).



      I have all the permissions to read/write.



      File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
      File destFolder = new File(Environment.DIRECTORY_DOCUMENTS + "/My Data/");
      if(!destFolder.exists())
      {
      // Make it, if it doesn't exit
      destFolder.mkdirs();
      }else{
      //TODO
      //delete the folder contents
      }


      // Create a TarInputStream
      try {
      TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
      TarEntry entry;

      while ((entry = tis.getNextEntry()) != null) {
      int count;
      byte data = new byte[2048];
      FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
      BufferedOutputStream dest = new BufferedOutputStream(fos);

      while ((count = tis.read(data)) != -1) {
      dest.write(data, 0, count);
      }

      dest.flush();
      dest.close();
      }

      tis.close();
      }catch (Exception e){
      Toast toast2 = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
      toast2.show();
      Log.e("TAG", "error", e);
      }


      Logcat says the error is on the first line after the try



      EDIT: for some reason it doesn't even create the /My Data/ folder even though it does go to destFolder.mkdirs(); when I'm debugging. not sure how it relates



      EDIT 2: I discovered that the tarFile.exists() and tarFile.canRead() says false, I tried to put an explicit path "storage/emulated/0/Download/Data.tar" AND IT WORKS, it exists and readable.



      Is there any danger leaving it like this? would some phones have a different ...emullated/X/... number?







      java android tar






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 23:35







      Ron

















      asked Nov 24 '18 at 22:21









      RonRon

      216




      216
























          2 Answers
          2






          active

          oldest

          votes


















          0














          If the android version of your device is larger than 6.0, only declare permissions request in AndroidManifest xml is not enough. You should also explicitly request before any operations which need permission.



          if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
          // Permission is not granted
          if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
          Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
          // Show an explanation to the user *asynchronously* -- don't block
          // this thread waiting for the user's response! After the user
          // sees the explanation, try again to request the permission.
          } else {
          // No explanation needed; request the permission
          ActivityCompat.requestPermissions(thisActivity,
          new String {
          Manifest.permission.WRITE_EXTERNAL_STORAGE
          },
          MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
          }
          }





          share|improve this answer
























          • I did it at the start of the activity and just to make sure I used your code (+read permission) to check again, and I had all the permissions

            – Ron
            Nov 24 '18 at 23:13











          • @Ron create folder/file need 'write' permission

            – navylover
            Nov 24 '18 at 23:29











          • yeah but reading the data.tar needs also read premission. and i just checked and using an explicit path "/storage/emulated/0/Download/Data.tar DOES find the file and it says it exists and readable

            – Ron
            Nov 24 '18 at 23:31



















          0














          I SOLVED IT!



          File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");



          will try to get a relative path from your app, even if you will write tarFile.getAbsolutePath().



          The solution?
          File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Data.tar");






          share|improve this answer
























          • not that the untar part of the code even works :/ but now I feel free again after 4(?) hours

            – Ron
            Nov 24 '18 at 23:51











          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',
          autoActivateHeartbeat: false,
          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%2f53462857%2fandroid-fileinputstream-exception-file-not-found%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          If the android version of your device is larger than 6.0, only declare permissions request in AndroidManifest xml is not enough. You should also explicitly request before any operations which need permission.



          if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
          // Permission is not granted
          if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
          Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
          // Show an explanation to the user *asynchronously* -- don't block
          // this thread waiting for the user's response! After the user
          // sees the explanation, try again to request the permission.
          } else {
          // No explanation needed; request the permission
          ActivityCompat.requestPermissions(thisActivity,
          new String {
          Manifest.permission.WRITE_EXTERNAL_STORAGE
          },
          MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
          }
          }





          share|improve this answer
























          • I did it at the start of the activity and just to make sure I used your code (+read permission) to check again, and I had all the permissions

            – Ron
            Nov 24 '18 at 23:13











          • @Ron create folder/file need 'write' permission

            – navylover
            Nov 24 '18 at 23:29











          • yeah but reading the data.tar needs also read premission. and i just checked and using an explicit path "/storage/emulated/0/Download/Data.tar DOES find the file and it says it exists and readable

            – Ron
            Nov 24 '18 at 23:31
















          0














          If the android version of your device is larger than 6.0, only declare permissions request in AndroidManifest xml is not enough. You should also explicitly request before any operations which need permission.



          if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
          // Permission is not granted
          if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
          Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
          // Show an explanation to the user *asynchronously* -- don't block
          // this thread waiting for the user's response! After the user
          // sees the explanation, try again to request the permission.
          } else {
          // No explanation needed; request the permission
          ActivityCompat.requestPermissions(thisActivity,
          new String {
          Manifest.permission.WRITE_EXTERNAL_STORAGE
          },
          MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
          }
          }





          share|improve this answer
























          • I did it at the start of the activity and just to make sure I used your code (+read permission) to check again, and I had all the permissions

            – Ron
            Nov 24 '18 at 23:13











          • @Ron create folder/file need 'write' permission

            – navylover
            Nov 24 '18 at 23:29











          • yeah but reading the data.tar needs also read premission. and i just checked and using an explicit path "/storage/emulated/0/Download/Data.tar DOES find the file and it says it exists and readable

            – Ron
            Nov 24 '18 at 23:31














          0












          0








          0







          If the android version of your device is larger than 6.0, only declare permissions request in AndroidManifest xml is not enough. You should also explicitly request before any operations which need permission.



          if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
          // Permission is not granted
          if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
          Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
          // Show an explanation to the user *asynchronously* -- don't block
          // this thread waiting for the user's response! After the user
          // sees the explanation, try again to request the permission.
          } else {
          // No explanation needed; request the permission
          ActivityCompat.requestPermissions(thisActivity,
          new String {
          Manifest.permission.WRITE_EXTERNAL_STORAGE
          },
          MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
          }
          }





          share|improve this answer













          If the android version of your device is larger than 6.0, only declare permissions request in AndroidManifest xml is not enough. You should also explicitly request before any operations which need permission.



          if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
          // Permission is not granted
          if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
          Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
          // Show an explanation to the user *asynchronously* -- don't block
          // this thread waiting for the user's response! After the user
          // sees the explanation, try again to request the permission.
          } else {
          // No explanation needed; request the permission
          ActivityCompat.requestPermissions(thisActivity,
          new String {
          Manifest.permission.WRITE_EXTERNAL_STORAGE
          },
          MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 22:54









          navylovernavylover

          3,42021118




          3,42021118













          • I did it at the start of the activity and just to make sure I used your code (+read permission) to check again, and I had all the permissions

            – Ron
            Nov 24 '18 at 23:13











          • @Ron create folder/file need 'write' permission

            – navylover
            Nov 24 '18 at 23:29











          • yeah but reading the data.tar needs also read premission. and i just checked and using an explicit path "/storage/emulated/0/Download/Data.tar DOES find the file and it says it exists and readable

            – Ron
            Nov 24 '18 at 23:31



















          • I did it at the start of the activity and just to make sure I used your code (+read permission) to check again, and I had all the permissions

            – Ron
            Nov 24 '18 at 23:13











          • @Ron create folder/file need 'write' permission

            – navylover
            Nov 24 '18 at 23:29











          • yeah but reading the data.tar needs also read premission. and i just checked and using an explicit path "/storage/emulated/0/Download/Data.tar DOES find the file and it says it exists and readable

            – Ron
            Nov 24 '18 at 23:31

















          I did it at the start of the activity and just to make sure I used your code (+read permission) to check again, and I had all the permissions

          – Ron
          Nov 24 '18 at 23:13





          I did it at the start of the activity and just to make sure I used your code (+read permission) to check again, and I had all the permissions

          – Ron
          Nov 24 '18 at 23:13













          @Ron create folder/file need 'write' permission

          – navylover
          Nov 24 '18 at 23:29





          @Ron create folder/file need 'write' permission

          – navylover
          Nov 24 '18 at 23:29













          yeah but reading the data.tar needs also read premission. and i just checked and using an explicit path "/storage/emulated/0/Download/Data.tar DOES find the file and it says it exists and readable

          – Ron
          Nov 24 '18 at 23:31





          yeah but reading the data.tar needs also read premission. and i just checked and using an explicit path "/storage/emulated/0/Download/Data.tar DOES find the file and it says it exists and readable

          – Ron
          Nov 24 '18 at 23:31













          0














          I SOLVED IT!



          File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");



          will try to get a relative path from your app, even if you will write tarFile.getAbsolutePath().



          The solution?
          File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Data.tar");






          share|improve this answer
























          • not that the untar part of the code even works :/ but now I feel free again after 4(?) hours

            – Ron
            Nov 24 '18 at 23:51
















          0














          I SOLVED IT!



          File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");



          will try to get a relative path from your app, even if you will write tarFile.getAbsolutePath().



          The solution?
          File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Data.tar");






          share|improve this answer
























          • not that the untar part of the code even works :/ but now I feel free again after 4(?) hours

            – Ron
            Nov 24 '18 at 23:51














          0












          0








          0







          I SOLVED IT!



          File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");



          will try to get a relative path from your app, even if you will write tarFile.getAbsolutePath().



          The solution?
          File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Data.tar");






          share|improve this answer













          I SOLVED IT!



          File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");



          will try to get a relative path from your app, even if you will write tarFile.getAbsolutePath().



          The solution?
          File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Data.tar");







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 23:49









          RonRon

          216




          216













          • not that the untar part of the code even works :/ but now I feel free again after 4(?) hours

            – Ron
            Nov 24 '18 at 23:51



















          • not that the untar part of the code even works :/ but now I feel free again after 4(?) hours

            – Ron
            Nov 24 '18 at 23:51

















          not that the untar part of the code even works :/ but now I feel free again after 4(?) hours

          – Ron
          Nov 24 '18 at 23:51





          not that the untar part of the code even works :/ but now I feel free again after 4(?) hours

          – Ron
          Nov 24 '18 at 23:51


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462857%2fandroid-fileinputstream-exception-file-not-found%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







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks