getExternalFilesDir(null) returns null - android

Once again, I've come up against a question which has been asked and answered before but in my implementation it is still not working.
I'm calling getExternalFilesDir(null) right at the very start of my main activity's onCreate method. It returns null every time, whether I run it in an AVD or on my phone (Samsung Galaxy Plus).
Yes, I have the <uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" /> line in my AndroidManifest.xml and yes, I am checking the external storage state before I make the call and it is mounted.
Here are the first three lines inside my onCreate() method. Actually, it's just after the super.onCreate() and setContentView() calls.
String state = Environment.getExternalStorageState();
File extFiles = getExternalFilesDir(null);
File locFiles = getFilesDir();
So, once these three lines have executed, these are the values for the variables:
state == "mounted"
extFiles == null
locFiles == "/data/data/com.mypackage.name/files"
Would anyone have any ideas as to why this might be?
-----EDIT-----
So I've tried another approach; Rather than using getExternalFilesDir(null), I tried using File basePath = new File(Environment.getExternalStorageDirectory(), "myAppName");
This is not ideal and I know that the Android documentation says, and I agree with it, that you should rather use getExternalFilesDir(). Seeing as that's not working for me though I had to try something else. This time the function does return a valid File object so, after the above line, the path of basePath is /mnt/sdcard/myAppName. So far, so good. When I check with DDMS I can see that /mnt/sdcard exists but not /mnt/sdcard/myAppName. This is to be expected. So I call boolean result = basePath.mkdirs();
But this returns false and when I check on the file system I can confirm that the myAppName subfolder has not been created. When I create the folder manually through DDMS and put files in it, I can read those files from my application but I can't write anything in that folder.
Please help! I'm at my wit's end.

If this wasn't a typo when you posted your question, you'll probably hate yourself for this:
<uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" />
should be
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

This is from Android documentation:
Returns the path of the directory holding application files on external storage.
Returns null if external storage is not currently mounted so it could not ensure
the path exists; you will need to call this method again when it is available.
The other option is you can check if External storage is available:
String state = Environment.getExternalStorageState();
File filesDir;
// Make sure it's available
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
filesDir = getExternalFilesDir(null);
} else {
// Load another directory, probably local memory
filesDir = getFilesDir();
}

My issue was that I opened a FileOutputStream, then before I closed the FileOutputStream, I opened a FileInputStream to see what was already in the file.
I moved opening the FileInputStream to before the FileOutputStream is opened and that fixed my issue.

Delete a line
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
in AndroidManifest.xml.
Switch a xml editor to "Android Manifest Permissions" with "Permission" tab in eclipse, and add a
uses-permission "android.permission.WRITE_EXTERNAL_STORAGE"
with some clicks.
Then try running your application.
It seems eclipse (may depends on a defference of version or state or settings) can ignore some words described by direct xml in AndroidManifest.xml.
Thanks for an advise. You are right, my answer looked like to agree in small talk.

Related

Deleting a File in Android returns false

I'm trying to delete a file in my Android app. The file was preciously created by the same Android app, and has MODE_WORLD_READABLE permissions.
I'm trying to delete it as follows:
File chosenFile = context.getFileStreamPath("myfile.txt");
boolean fileDeleted = chosenFile.delete();
if (fileDeleted)
Log.d(TAG, "myfile.txt was deleted");
else
Log.d(TAG, "myfile.txt was not deleted");
chosenFile.delete() keeps returning false. Is it because it's still being accessed? If so, is there any way I can force close it?
Thanks.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Did you give this permission.
for this line of code is myfile.txt exists, please check that first, file write permissions are not required for writing in private file system. Its required only when you try to write external storage.
context.getFileStreamPath("myfile.txt");
and to delete the file use
context.deleteFile("myfile.txt");
OK, so I figured out my mistake - the file doesn't exist in the first place. I thought I guarded against that by checking if (chosenFile == null), but I should've checked if (chosenFile.exist() == true) instead.

Check if a file has been written to in Android

I have a question about Android programming. Basically, I am unsure of where to check where my file is, and if I wrote to it correctly. I want to locate where the file is, and I also want to know whether or not I wrote to it correctly. Below is the code I have come up with:
String lsNow = "testing";
try {
fos = openFileOutput("output.txt", Context.MODE_APPEND);
fos.write(lsNow.getBytes());
fos.close();
}
catch{
...
}
Where can I find output.txt? Might anyone know how to check this all out? if so, that would be great! I am using an emulator by the way. If I were to do this on a real Android, how would one approach this also? (Just for future reference)
You Test it in Two ways
Using File Explorer
Go to DDMS perspective--> Open File Explorer-->location of the file
Pragrammatically by using exits() method
File file = new File(context.getFilesDir(), filename);
if(file.exists())
Using openFileOutput(...) means the file will be written to internal storage on the Android device in an area which is secure from access by other apps.
If you want to make sure the file is written correctly then make sure your catch block handles any failures (if it is called then the file writing has failed).
To access the file once it has been written use openFileInput(...).

Deleting file from private storage on android

I'm trying to delete a file that I earlier created in my android app.
The problem I'm having is that the file won't go away. Even though everything seems to work.
I've looked at several post here on stackoverflow, but still not solution. The garbage collections was one of the hints I've found.
System.gc();
System.out.println("Exists: "+file.exists());
System.out.println("Read: "+file.canRead());
System.out.println("Write: "+file.canWrite());
System.out.println("Deleting: " + file);
boolean r = file.delete();
System.out.println("Result of deletion: "+r);
System.gc();
And the result in the log
Exists: true
Read: true
Write: true
Deleting: data/data/no.ntnu.kpro.app/files/kprothales/XOMessage/8
Result of deletion: true
Does anyone have any idea as to why it isn't removed?
EDIT:
Lucifer: Yeah, I have set WRITE_EXTERNAL_STORAGE permission in the manifest.
ShineDown: No, it is just a file without an extension. For now it is containing xml, but this is going to change over time, hence why I have not called it .xml. Could this be a problem?
chintan khetiya: I believe this line is allready included in the code above.
check the answer here:
Android: how to delete internal image file
which is basically suggesting to call deleteFile:
if(activity.deleteFile(imageName))
Log.i(TAG, "Image deleted.");

Android Permissions Issue

I am writing a simple activity to record and save audio, preferably to a folder within my application, but, for simplicity, to the SD card. The line of code that's giving me trouble is
String path = Environment.getExternalStorageDirectory().toString() + "/" + "tempAppFiles/";
String filename = "test"+".mp4";
recorder.setOutputFile(path + filename);
where recorder is an instance of MediaRecorder.
When I run the application, I get a permissions error that states
07-31 15:51:51.810: W/System.err(13670): java.io.FileNotFoundException: /mnt/sdcard/tempAppFiles/test.mp4 (Permission denied)
I looked this problem up and found that I needed to add several permission tags to my manifest, and I added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
to my manifest.
I am still getting the same permissions issue, and I can't find anyone with a similar problem.
Any ideas?
Hmm... The permissions seem to be correct, perhaps you've put them in the wrong place, they need to be children of the root note .
Next you can check, whether you can write the file by checking
boolean canIWrite = path.canWrite();
As you also get a FileNotFound exception you could try...
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
...instead of...
String path = Environment.getExternalStorageDirectory().toString();
If that is no help at all, there is still an official example of capturing audio - you should compare your code to:
http://developer.android.com/guide/topics/media/audio-capture.html
tempAppFiles does not set well with me. Seems like you should be writing to a directory that is under the package name of the app you are writing ...

Android: external storage permission issue:

I really need someone here to guide me about some issues with the external storage OR sd-card. I won't go into complexities. I have a folder with the name of MyVideos. It is located in the sd-card folder of Motrola Xoom; the path is "/mnt/sdcard-ext/MyVideos. The folder is already there. However, there are some strange errors I am having.. For example, if I check if the folder exists or not using the following code:
File myDirectory = new File(defaultStorage, "/MyVideos/");
if (myDirectory.exists())
{
my code: lets say true
}
else
{
my code: false
}
where defaultStorage is = "/mnt/sdcard-ext". It always return false. It should return true since the folder surely exists there. And assuming that the folder exists there, I perform other operations like
for (File f : myDirectory.listFiles())
{
if (f.isFile())
{
filenames.add(f.getName()); //add to array
}//if closes
}//for closes
I get FATAL exception at the following line
for (File f : myDirectory.listFiles())
The error stack is below:
E/AndroidRuntime(22644): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
E/AndroidRuntime(22644): Caused by: java.lang.NullPointerException
I have just mentioned the things that are important from the stack. It is a NullPointerException to be precise. Although I am using
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in my manifest file. Even then it doesn't work. The most surprising element is that it is running fine here; but there is a client in USA, it is crashing on all his devices.. Is there any difference in the permission settings of devices in UK or in USA.. I shall be thankful to you if you help me here.. It is a big problem for me.
Thanks
According to the documentation, the state you're describing means that the external media storage isn't accessible to you:
public static final String MEDIA_SHARED
Added in API level 1 getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage.
Constant Value: "shared"
You need to go to your USB Mass Storage options and turn off USB storage.
PS : Thanks to DigCamara I copied from this SO answer.
Thanks

Categories

Resources