téléchargement mp3 application podcast
Bonjour a tous,
je suis nouveau dans la communauté et commence à programmer avec android depuis peu.
Je développe une application rss/podcast pour Android et j'ai quelques problèmes concernant le téléchargement du .mp3 en ligne.
J'ai une classe appellante : ListItems.java qui appelle la méthode de téléchargement quand on click sur l'Item demandé.
L'object est bien récupéré et le lien est bon (String contenant l'url du mp3)
Code:
1 2 3 4 5 6
|
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
ImageManager manager = new ImageManager();
manager.downloadFeed(messages.get(position).getLink());
} |
Ensuite, le ImageManager (qui porte mal son nom depuis les changements) avec la méthode downloadFeed(String link):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
public void downloadFeed(String link) {
HttpURLConnection c;
FileOutputStream f = null;
availabilityMMC(); //sets the boolean for the availability of memorycard
if (!mExternalStorageWriteable) { // if storage not available, no need
Log.i("ImageManager", "storage card not writable"); // to save the file
return;
}
URL url = null;
try {
url = new URL(link);
} catch (MalformedURLException e1) {
Log.e("error", "malformed URL");
e1.printStackTrace();
}
try {
c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
f = new FileOutputStream(new File(
Environment.getExternalStorageDirectory(), "podcast.mp3"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[4096];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
} catch (IOException e) {
Log.e("error", "io exception");
e.printStackTrace();
} finally {
try {
f.close();
} catch (IOException e) {
Log.e("error", "io exception");
e.printStackTrace();
}
}
} |
Le résultat crée bien un fichier podcast.mp3, mais il est illisible.
J'aurai aussi aimé le mettre dans le dossier sdcard/feed/MaChannel/ mais je n'y arrive pas non plus.
Si quelqu'un peut m'aider je vous en serais reconnaissant :cry::mrgreen:
Cedric