I'm trying to access the two files, Spring v2.json and Test.json, in my Android app. However, I will add them using Windows with my phone connected, but when I run my app, the file seems to disappear.
Getting the file
File file = new File(getExternalFilesDir(null), "Spring v2.json");
Check is the file exists
if (file.exists()) {
TransferObserver observer = transferUtility.download(
"easelbucket", // bucket to download from
"sections/" + objectKey, // key for object to be downloaded
file // file to download object to
);
} else {
Toast.makeText(this, "File does not exist", Toast.LENGTH_SHORT).show();
return null;
}
I know that the file stops existing because (1) the if statement enters the else block, and (2) the app crashes when it attempts to use the result of the file.
File file = new File(getExternalFilesDir(null), "/Spring v2.json")
You forget slash before filename.
Also check if
.canRead();
and
.canExecute();
Related
I put some .mp4 file in a folder in my android device internal storage. And then I play this file from my application. It plays well if exists otherwise app crushed.
So before play file I want to check its existence.
I tried below code but no luck.
File file = new File("file:///storage/emulated/0/tutorial/1a1cbfc4-18cb-4637-8405-01bf9bebeda3.mp4");
if (file.exists()) {
LogUtil.printLogMessage(VideoListActivity.class.getName(), "video File", "file exist");
} else {
LogUtil.printLogMessage(VideoListActivity.class.getName(), "video File", "file not exist");
}
i am sure the file is exist in the folder named tutorial in my internal storage.
Use file.isFile() for file & file.isDirectory() for directory,
file.exists() tries to access the file which causes it to crash if the file doesn't exist, file.isFile() uses linux stat, which only returns the information about the file without trying to access it.
Please make proper file name, Replace with File myDirectory = new File(Environment.getExternalStorageDirectory(), dirName);
public Boolean checkFile(){
File file = new File("/storage/emulated/0/tutorial/1a1cbfc4-18cb-4637-8405-01bf9bebeda3.mp4");
if (file.exists()) {
return true;
} else {
return false;
}
}
Use checkfile True/False flag before video play.
Try this
File file = new File(path);
if (!file.isFile()) {
}else{
}
I have been successfully able to create files in my apps local directory using the below code
File appDir = new File(this.getExternalFilesDir(null), "my_folder");
if (!appDir.exists())
{
boolean result = appDir.mkdir();
if (!result) {
Log.i(Util.TAG, LOG_LABEL
+ ":: Unable to create \"my_folder\" Directory : "
+ appDir.getAbsolutePath()
+ " Directory already exists !");
}
}
File HTMLFile = new File(appDir,"html.txt");
But now when I tried to do the same from a service, the file was not being created, I even checked using 'HTMLFile.exists()' and it says the file does not exist.
My question is, Is it actually possible to create files from a service? or am I missing something here.
It is possible to write to the storage of your android: by the help of the following methods
Make sure you provide uses permission to write and read from storage in manifest file.
I have used this code:
private File newFile;
public ListenNotification() {
Log.d("Service","InConstrutor");
newFile=newFile(Environment.getExternalStorageDirectory(),"NotificationFromService");
if(!newFile.exists()){
Log.d("Service","Created");
newFile.mkdir();
}
else{
Log.d("Service","Existed");
}
}
File HTMLFile = new File(newFile,"html.txt");
I created a file by file.createNewFile() command in "data/data/com.android.bonvoyage" folder to test file creation in the internal storage of my android tablet.
I found that the file should be visible when I have root account, but I want to find a way
to see the file created without root permission.
I don't care where the file is created, just want to see and test it on actual tablet.
Can I do that?
The process was successful by
File file = new File("data/data/com.android.bonvoyage/myfile.txt");
boolean tf = false;
if (!file.exists()) {
try {
tf = file.createNewFile();
} catch (IOException ioe) {
Toast.makeText(this, ioe.toString(), 5000).show();
//ioe.printStackTrace();
} finally {
Toast.makeText(this, "File Created? " + Boolean.toString(tf), Toast.LENGTH_SHORT).show();
}
}
With root explorer or any file explorer that supports root you could browse to: data/data/com.android.bonvoyage/myfile.txt and look whether its there or not?
Write the file to the sdcard and then you can see it with the file browser without root permissions.
Use the following code (taking from the android guide) to open a file in the pictures folder for example:
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
The full guide can be found here:
http://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage
How can we check the given file is present in Android Filesystem or not without using File inputstream.because I only want to check the existence of the file on the given path.
File myFile = new File("/path/to/file");
if (myFile.isFile()) {
...
}
If you have created a file using openFileOutput method then this works
File mFile = new File(getBaseContext().getFilesDir(),FileName);
if (mFile.exists() )
{
//File is Exits.
}
else
{
//File is not Exits.
}
hope this helps
My program checks if a specific file exists in the default cache folder. If the file exists, it opens it and reads the contents. If the file does not exist, the file gets pulled from the web and is stored in the cache folder. The problem I'm having is that, no matter if the file is in the cache folder or not, my file test always returns false. The funny thing is that, even though the file test returns false, I can still open the file from the cache folder and read it. I can pull a list of files in the cache folder and I can see the file is there, but when I do the file test to see if the file is there, it returns false, even though I know the file is there and I can open it and see it's contents.
I tried the regular exists() test and even reading each file in the cache directory one by one and comparing the name to the file I'm looking for and still returns false.
Thanks for any help in advance!
String file = "test.txt"
String content = "testing";
putFile(file, content);
Boolean fileIsThere = checkFile(file);
public Boolean checkFile(String file){
Boolean fileExists = false;
// regular file test - always returns false, even if the file is there
File f = new File(file);
if (f.exists())
fileExists = true;
// comparing each individual file in the directory - also returns false
String[] dirFiles = fileList();
for (int i = 0; i < dirFiles.length; i++) {
if (dirFiles[i] == file){
fileExists = true;
break;
}
}
return fileExists;
}
public void putFile(String file, String content){
try {
FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
Log.w("putFile", "Error (" + e.toString() + ") with: " + file);
}
}
Any ideas? I'm thinking that since I'm putting the files in the cache folder, I will always get false on the file test. I just want to see if anyone else came across this and has a fix for it, or if I have to make a specific directory and store my files there, or something else. Could "Context.MODE_PRIVATE" in putFile() have anything to do with it?
If you want to test existention of file stored in your Context storage data/data/nameOfPackage/files/text.txt you have to rewrite String file like this
String file = "/data/data/nameOfPackage/files/test.txt"
Then you can check exists() method of your test.txt file. I hope it will help you. :)
if (f.exists() && f.length() > 0) fileExists = true;
This works for me!