Pippo's blog

it is all about software development

This is annoying

You can read the title as “How to make your recently created android app’s file visible”.

I am recetly playing around with Android Platform and around goods and bads this is just annoying. Android has an complex user mode mecanism to segregate application namespace. It is based on linux user mode and each application has its own, well, user. It is OK and brings up a good security policy but has some trade offs as well.

Going specific to what I want to say about files: You do have many ways to write a file with your app to the external storage media. External media being the same place you see your files when you download any the browser.
Once you have your file writen it doesn’t mean that you gonna find it through any file explorer when naviagting to your phone storage using your computer. You find the file using a android file manager within the phone though.

So, tell me what is annoying, damn!

This is annoying: to make your file let’s say ‘visible’, you have to run this code:

1
MediaScannerConnection.scanFile(context, new String[] {filePath}, null, null);

Why? I don’t know! It is the way it was thought. I think this is stupid! I didn’t want to dig further so I stopped here!

Just one more tip, for easy file handling, use apache commons-io. Add this to your pom.xml if using maven:

1
2
3
4
5
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

Than you are able to use it like this:

1
2
3
4
5
import org.apache.commons.io.FileUtils;

FileUtils.readFileToString(new File(filePath));

FileUtils.write(file, txt);

Comments