Android,
A Complete Course, From
Basics to Enterprise Edition
Android, A Complete Course, From Basics to Enterprise Edition
Cet
article est extraie du livre « Android, A Complete Course »,
disponible sur Android2ee.com.
Les
exemples ou les programmes présents dans cet ouvrage sont fournis pour
illustrer les descriptions théoriques. Ce code est libre de toute utilisation
mais n'est pas distribuable.
La
distribution du code est reservée au site :
La
propriété intellectuelle du code appartient à :
L’utilisation
de ces codes est sous votre unique responsabilité et personne d’autre que vous
ne pourra être tenu responsable des préjudices ou dommages de quelques natures
que ce soit pouvant résulter de son utilisation.
Tous
les noms de produits ou marques cités dans cet ouvrage sont des marques déposés
par leurs propriétaires respectifs.
Publié par http://android2ee.com
Titre Original : Android, A Complete Course, From
Basics to Enterprise Edition. Édition Française.
ISBN : 979-10-90388-00-0
Copyright © 2011 by Mathias Séguy
Aucune
représentation ou reproduction, même partielle, autre que celles prévues à
l’article L. 122-5 2° et 3° a) du code de la propriété intellectuelle ne peut
être faite sans l’autorisation expresse de Mathias Seguy ou, le cas échéant,
sans le respect des modalités prévues à l’article L. 122-10 dudit code
Les fichiers se scindent en deux groupes, ceux qui sont fournis par l’application (et sont donc read only) et ceux qui sont créés sur le terminal.
Ces fichiers sont déposés par convention dans le dossier res\raw\ et on y accède dans le code de la manière usuelle par R.RAW.MonFichier :
InputStream inputStream = getResources().openRawResource(R.raw.internal_file);
Vous pouvez alors le lire en toute simplicité :
/***************************************************************************************/
/** Simple File reader
*********************************************************************/
/***************************************************************************************/
// First implement the textView
TextView
textViewSelected = (TextView) findViewById(R.id.TextViewSeletedFile);
textViewSelected.setText(fileName);
// First implement the textView
TextView
textViewFile = (TextView) findViewById(R.id.TextViewFileReader);
// Read the file :open a InputStream on it
InputStream
inputStream = getResources().openRawResource(R.raw.internal_file);
try {
if (inputStream != null) {
//open a reader on the inputStream
BufferedReader reader = new
BufferedReader(new
InputStreamReader(inputStream));
//String used to store the lines
String str;
StringBuilder buf = new
StringBuilder();
//read the file
while
((str = reader.readLine()) != null) {
buf.append(str + "\r\n");
}
//close streams
reader.close();
inputStream.close();
textViewFile.setText(buf.toString());
}
}
catch
(java.io.FileNotFoundException e) {
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG);
}
catch
(IOException e) {
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG);
}
Si votre fichier est un fichier xml, l’exemple suivant permet de comprendre comment le manipuler :
/************************************************************************************/
/** Xml File reader ********************************************************************/
/************************************************************************************/
// First implement the textView
TextView
textViewXmlSelected = (TextView) findViewById(R.id.TextViewSeletedXmlFile);
textViewXmlSelected.setText(xmlFileName);
// First implement the textView
TextView
textViewXmlFile = (TextView) findViewById(R.id.TextViewXmlFileReader);
// Read the file
inputStream = getResources().openRawResource(R.raw.internal_xml_file);
try {
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(inputStream, null);
NodeList nodes = doc.getElementsByTagName("string");
StringBuilder buf = new
StringBuilder();
Element element;
for (int i = 0; i < nodes.getLength(); i++) {
element =
(Element) nodes.item(i);
buf.append("Node name:"+element.getNodeName());
buf.append("\r\n\t Node Attribute name :"+element.getAttribute("name"));
buf.append("\r\n\t Node Text Content:"+element.getTextContent());
buf.append("\r\n\t node type:"+element.getNodeType());
buf.append("\r\n");
}
inputStream.close();
textViewXmlFile.setText(buf.toString());
}
catch
(java.io.FileNotFoundException e) {
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG);
}
catch
(IOException e) {
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG);
}
catch
(ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Où le fichier xml est de ce type (et se nomme internal_xml_file.xml) :
<resources>
<!--Simple string-->
<string name="hello">Hello World, ResourcesTuto!</string>
<string name="app_name">ResourcesTuto</string>
<string name="simpleString">Good Morning My name is
Bond, James Bond</string>
<!--String with parameters ($s for string, $d for decimal (any
decimal:integer, double, float...)-->
<string name="param_message">Hello, i love %1$s, i
hate %2$s, i am %3$d years old</string>
<string name="spinner">Do you like: </string>
<!--String array-->
<string-array name="i_like_array">
<item>chocolate</item>
<item>television</item>
<item>internet</item>
<item>nicotine</item>
<item>hug</item>
<item>Santa Claus</item>
</string-array>
<!--Plurals String-->
<plurals name="singleOrPlural">
<item quantity="one">I am alone on moon</item>
<item quantity="other">I am 6 900 000 000 on earth</item>
</plurals>
<string name="button">An image on the button</string>
</resources>
Ces fichiers sont privés à votre activité. Vous pouvez les ouvrir en lecture ou en écriture (mais pas les deux) via les méthodes openFileInput(String filename) ou openFileOutput(String fileName). Comme ces fichiers sont propres à votre activité, Android gère leur emplacement, il est donc interdit d’utiliser des chemins pour le fileName et leur extension. Il faut juste mettre le nom du fichier toto et pas .\DossierToto\toto.txt.
Quand les fichiers ont fini d’être utilisés, il faut les fermer, dans la méthode onPause de votre activité. Celle dont on est sûr qu’elle sera appelée avant la destruction de votre activité.
Pour parcourir la liste des fichiers de votre application, la méthode String[] fileList() est à votre disposition. Pour supprimer un fichier utilisez la méthode deleteFile(FileName). Ces méthodes peuvent être utiles pour nettoyer vos fichiers dans les cas où votre application se termine de manière inopinée (i.e. potentiellement tout le temps).
Un dernier détail d’importance deux modes d’ouverture sont disponibles pour l’ouverture en écriture des fichiers, le mode Private qui écrase le contenu existant du fichier et le mode Append qui concatène vos données à la fin du fichier :
openFileOutput(NOTES, Context.MODE_PRIVATE)
openFileOutput(NOTES, Context.MODE_APPEND)
Ainsi nous obtenons le code suivant :
Pour la lecture :
// First instanciate and fill the spinner with existing files
spinner = (Spinner) findViewById(R.id.SpinnerFiles);
ArrayAdapter<String>
adapter
= new
ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
fileList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
// Instanciate the first element
String
fileName
= (String) spinner.getSelectedItem();
//if there is a selected file, then read it
if (fileName != null) {
try {
//open the file and retrieve the inputStream
InputStream inputStream = openFileInput(fileName);
if
(inputStream
!= null)
{
//open a reader on the inputStream
BufferedReader
reader
= new
BufferedReader(new
InputStreamReader(inputStream));
//String to use to store read lines
String
str;
StringBuilder
buf = new
StringBuilder();
//read the file
while ((str = reader.readLine()) != null) {
buf.append(str + "\r\n");
}
//close the reader
reader.close();
//close the inputStream
inputStream.close();
//Affect the text to the textView
textViewFile.setText(buf.toString());
}
} catch
(java.io.FileNotFoundException e) {
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG);
} catch
(IOException e) {
Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG);
}
}
Pour l’écriture :
String
fileNameStr="MyFileName";
String
fileContentStr="The
content of the file …";
try{
//To open you can choose the mode MODE_PRIVATE, MODE_APPEND,
//
MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE
//This is the creation mode (Private, World Readable et World Writable),
//Append is used to open the file and write at its end
FileOutputStream fos= openFileOutput(fileNameStr,
Context.MODE_PRIVATE);
//open the writer
OutputStreamWriter outputStreamWriter=new
OutputStreamWriter(fos);
//write
outputStreamWriter.write(fileContentStr);
//Close streams
outputStreamWriter.close();
fos.close();
}catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
L’accès aux fichiers externes de votre application s’effectue au moyen d’un Content Provider qui encapsule cet accès.
L’exemple ci-dessous montre l’utilisation du MediaStore pour récupérer la liste des chansons du lecteur externe :
//Define the URI for the external audio files
Uri
media =
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//Define the data you want to gather
String[]
projection
= { MediaStore.Audio.Media._ID, // 0
MediaStore.Audio.Media.ARTIST, // 1
MediaStore.Audio.Media.TITLE, // 2
MediaStore.Audio.Media.ALBUM}; // 3
//Define the where clause (which audio files do you want)
String
selection
= MediaStore.Audio.Media.IS_MUSIC + " !=
0";
//make the query
Cursor
cursor
= getContentResolver().query(media,projection,selection,null,null);
StringBuilder
strbuilder=
new
StringBuilder();
//browse the result of the query and do something with it:
while(cursor.moveToNext()){
strbuilder.append("A
new song from ");
strbuilder.append(cursor.getString(1));
strbuilder.append("
called ");
strbuilder.append(cursor.getString(2));
strbuilder.append("
in the Album ");
strbuilder.append(cursor.getString(3));
strbuilder.append("\r\n");
}
//Display that list
AlertDialog.Builder
alertDialog=new AlertDialog.Builder(this);
alertDialog.setMessage(strbuilder.toString());
alertDialog.setPositiveButton("Close", new Dialog.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog,
int which)
{
}
});
alertDialog.show();