I've a problem with reading a XML file in my APP. I've tried multiple options but I'm getting error that read acces is false. I'm sure I'm using the correct path.
XML = new File(XML, "file.xml");
Log.i("XML", "Read access:" + XML.canRead());
This does return a false, I only need to read not to write (at least, not yet..).
System.getProperty(XML.getPath())
Returns null
I think there is a problem with the SD card (see other issue). But in my app I can open diffent folders except the XML file in the last folder.
I've added the correct permission to the manifest file.
Any help is very much appreciated. Thank you.
Edit:
File path:
File f = new File(Environment.getExternalStorageDirectory().toString()
+ File.separator
+ "external_sd"
+ File.separator
+ "app"
+ File.separator
+ "Games"
+ File.separator
+ "Version_1"
);
I've no problem with opening the folders. In my last folder there wil be a xml which can't be opened.
I'm using part of the path in other parts of the APP and there it works..
Also the code XML.exists() returns false...
Was related to to other issue, that one also solved this issue
all thanks for the help..
Related
I am looking for a maybe better solution to get the path of a file inside a folder.
For example:
I have the absolute path: "/storage/emulated/0/Mobile Pauker++/Lessons/Berge/Hoechste-Berge.pau.gz"
And I want to get: "Lessons/Berge/Hoechste-Berge.pau.gz"
My current solution is:
filepath.substring(filepath.indexOf("Mobile Pauker++")+15)
But I don't think that this is a good solution.
Please try this val path = File(Environment.getExternalStorageDirectory().toString() + File.separator + "Lessons/Berge/Hoechste-Berge.pau.gz")
I think I found a good solution:
filepath.split("${Environment.getExternalStorageDirectory()}${Constants.DEFAULT_APP_FILE_DIRECTORY}")[1]
I give the .split() method the path of my "root folder" and take the second part.
I'm trying to create an empty directory, but instead it creates a file.
publicDocsPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
autoTestDir = new File(publicDocsPath + File.separator+"autoTest");
autoTestDir.mkdirs();
I can get around it by creating a dummy file under neath it, as such
autoTestDir = new File(publicDocsPath + File.separator+"autoTest" + File.separator+ "nullfile");
But I would like to know if I'm doing something wrong or if there is a way to tell the system you want to create a directory not a file.
Note: I'm on a mac, and I'm using Android File Transfer program to verify my results. Maybe the file is being created as a directory, but issue is with Android File Transfer and it shows the directory as a file.
Try this
File documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File dir = new File(documents.getAbsolutePath() + File.separator + "autoTest" + File.separator);
// creates if doesn't exists
dir.mkdir();
You have to add the file separator after "autoTest".
This is my first Android app and my first attempt at writing something to file. I'm trying to capture a log according to these instructions and I'm getting the FileNotFoundExeption ENOENT (No such file or directory). That's fair enough because the directory does not exist. But then how do I create the directory? Or use another one? I don't know best practices for where to write logs to email them, nor do I know how to make a new directory for them.
This is the path I'm trying to use.
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
String fullName = path + "mylog";
File file = new File (fullName);
The parent dir doesn't exist yet, you must create the parent first before creating the file
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
// Create the parent path
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String fullName = path + "mylog";
File file = new File (fullName);
Edit:
Thanks to Jonathans answer, this code sample is more correct. It uses the exists() method.
You also need the permission in your manifest:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
I'd like to add to Francesco's answer, that instead of asking if it's a directory, you could ask if it exists with dir.exists() method.
And also check that you've set the proper permissions in the Manifest file.
Hope it helps
Jonatan
It's been two days now I'm stucked on a veeery common and simple problem I don't seem to be able to solve (while other people are):
creating a folder on a SD card on Android!!! YES!!!
I red many many posts here, many tutorials that seems to say the same thing:
create a string with the external folder path + /yourFolderName
create a new file passing the path as argument
call mkdir() (or mkdirs()) on it
done!
alongside you can check if the SD card is MOUNTED, READABLE, WRITABLE,
and of course don't forget to put in your Manifest.xml the permission to WRITE_EXTERNAL_STORAGE, but be careful, it must be set as direct child of the manifest and not of the application!!!
Well nothing of that seems to work for me.
The folder_creation code is inside onCreate() and I'm trying to call a MediaScannerConnection to test if the file exists but it return null on OnScanCompleteListener - but I'm not sure I'm using this in the correct way-.
The application runs fine (launch the default camera activity then returns to the main one), but the folder is not created! (by now I just want to create the folder with nothing inside)
Maybe is an issue related to the package name containing the word "example" included in my package name? (I red something related somewhere...)
What could be wrong? Please give me an hint, advice, something...
I'm building on a Mac 1.7.5 with Eclipse using minSdkVersion = 8 and testing on a HTC Wildfire S with 2.3.5 (sdkVersion = 10). Checking with ES File Manager 1.6.1.6 on a non rooted device
Here's the main part of the code driving me crazy...
I would be very glad to know there's something I can do...
Thank you in advance!
myPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
+ "myFolder";
File newDirectory = new File(myPath);
Log.v("judy says", newDirectory.toString());
newDirectory.mkdir(); // this doesn't work because "no directory" is displayed in the logCat window
if (!newDirectory.exists()) {
newDirectory.mkdir();
Log.v("no, no, no", "no directory");
}
MediaScannerConnection.scanFile(this,
new String[] { newDirectory.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
This works, if you want to create a directory "/mnt/sdcard/somedir/newdir"
File temp = new File (Environment.getExternalStorageDirectory (),
"somedir" + File.separator + "newdir");
if (!temp.exists ())
temp.mkdirs ();
ok, I solved: Simply using "uses permission" instead of "permission", the problem was that I was hard writing it in the manifest.xml, instead of using the graphical interface, this way I could use the built in list to choose a permission...
Really sorry if I bored...
I hope this could be useful for someone.
I want to save some data in the user's external directory (ie. SD card), but there seems to be a weird problem. I'm using Environment.getExternalStorageDirectory() which returns "mnt/sdcard/" (which is fine). I want to create two folders on in this directory so I do:
File main = new File(getExternalStorageDirectory() + "/my_app/some_data");
if(!main.isDirectory())
main.mkdirs();
Now I thought this would make the directory "mnt/sdcard/my_app/some_data", but after using a file manager to look at the SD card, it turns out that this folder is created at "mnt/sdcard/my_app/mnt/sdcard/my_app/some_data", which is quite bizarre. Can anyone tell me how to fix this?
Try the following and see what you get...
String packageName = this.getPackageName();
File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Android" + File.separator + "data" + File.separator + packageName + File.separator + "files");
myFilesDir.mkdirs();
It's exacly what I use to create a working directory on an SD card. For me it creates...
/mnt/sdcard/Android/data/com.mycompany.myApp/files
...where 'com.mycompany.myApp' is the actual package name of my app.