Android FileInputStream exception file not found
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
add a comment |
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
add a comment |
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
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
java android tar
edited Nov 24 '18 at 23:35
Ron
asked Nov 24 '18 at 22:21
RonRon
216
216
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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);
}
}
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
add a comment |
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");
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
}
}
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
add a comment |
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);
}
}
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
add a comment |
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);
}
}
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);
}
}
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
add a comment |
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
add a comment |
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");
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
add a comment |
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");
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
add a comment |
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");
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");
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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