Friday, April 01, 2011

Create folder/file on Android

Legalabbis a Samsung Galaxy S-en ezt tapasztaltam:

Az Eclipse File Explorer-eben is lathato az /mnt/sdcard/ folder. Ezen belul van egy external_sd ami mindig jelen van, de csak akkor hasznalhato, ha tenyleg beteszunk egy kulcso SD Card-ot a telefonba.
Ami videokat/kepeket a default Camera app-al rogzitunk az a
/mnt/sdcard/DCIM/Camera/video-yyyy-mm-dd-hh-mm-ss.mp4
illetve
/mnt/sdcard/DCIM/Camera/yyy-mm-dd hh.mm.ss.jpg
file-ba kerulnek.

Ha kodbol letrehuzzuk az "/folder/foo" folder-t, akkor az itt lesz:
/mnt/sdcard/foo
es nem torlodik, ha letoroljuk az app-ot ami csinlata.
Ezzel a koddal keszult:
File newDirectory = new File("/sdcard/foo/");
newDirectory.mkdirs();

getFilesDir() azt a folder-t adja vissza ahova az app irhat. Ez a folder az app torlesenel automatikusan torlodik es csak az adott app fer hozza.
Valahogy igy nez ki: /data/data/com.example.foo/files/

getExternalFilesDir(String type) is egy olyan folder-t ad vissza, ahova az app irhat csak ez az external SD kartyan. A dokumentacio szerint "These files are private to the applications, and not typically visible to the user as media" de nem tudom a "typically" itt mit jelent. A getFiles()-al szemben viszont "There is no security enforced with these files. All applications can read and write files placed here."
Valahogy igy nez ki: /mnt/sdcard/external_sd/Android/data/com.example.foo/files/
Tehat az /mnt/sdcard/external_sd/ ezert el akkor is, ha nincs a telefonban kulso SD kartya es nyugodtan lehet is hasznalni ezzel a method-dal. Peldaul letrehozni benne sajat bar konyvtarat a foo app konyvtara ala:
File efd = getExternalFilesDir(null);
File barFolder = new File(efd.getAbsolutePath() + "/bar/");
barFolder.mkdirs();
Ez igy rendben mukodik, de amikor uninstallaltam a foo app-ot, akkor sem torlodott az /mnt/sdcard/external_sd/Android/data/com.example.foo/files folder. Pedig a dokumentacio szerint: "This is like getFilesDir() in that these files will be deleted when the application is uninstalled"
A type parameter pedig erre jo: "The type of files directory to return. May be null for the root of the files directory or one of the following Environment constants for a subdirectory: DIRECTORY_MUSIC,DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, or DIRECTORY_MOVIES"

getExternalStorageDirectory(): "don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer."

"Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String)."