Reputation: 95
Here is my code:
String dir = getFilesDir().getAbsolutePath();
File myFile = new File(dir+"/file.apk");
if (myFile.exists())
{
textView.setText("File exists.");
}
else
{
textView.setText("File does not exist.");
}
myFile.exists() is false. I do not know why. The file exists and it is located in the directory.
When I solve the problem, I'll try this:
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(Uri.fromFile(myFile));
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Can somebody help? Why it does not see the file?
UPDATE:
It's really strange. If I use code:
if (myFile.exists())
{
textView.setText("it exists");
}
else
{
textView.setText(myFile.getAbsolutePath());
}
, it goes to 'else' and shows the path to the file which 'does not exist'.
Upvotes: 2
Views: 4064
Reputation: 2865
If you construct the file with the 2-arg constructor, you can avoid adding a system-dependent path separator character. Like this:
File myFile = new File(dir, "file.apk");
Upvotes: 0
Reputation: 95
Thanks to greenapps:
"Please click in Astro app left to the word Primary on the up arrow to see the real path. /Primary/ does not exist on an Android device. It's an Astro invention. And Astro shows external memory with Primary. And take a better file explorer like ES File Explorer to inform you about real paths"
I used direct path I found using Astro (modified string dir to '/sdcard/data/data/...").
Upvotes: 1
Reputation: 33438
Try using this code:
String dir = getFilesDir().getAbsolutePath();
boolean fileExists = (new File(dir + "/file.apk")).isFile();
if (fileExists)
{
// your file exists
}
else
{
// your file does not exist
}
Upvotes: 0