Android : Download images and mp3 audio from server -
i want download mp3 audio , images server.file downloaded not goes sd card folder.below code correct or not or wrong in downloading on store sd card folder.how work download media files , save sd card folder.thanks in advanced.
here mp3 download code.
public void downloadaudiofile(final string mp3url , final string strimagename) { new asynctask<string, string, string>() { @override protected string doinbackground(string... f_url) { int count; try { f_url[0] = mp3url; url url = new url(f_url[0]); urlconnection conection = url.openconnection(); conection.connect(); // music file length int lenghtoffile = conection.getcontentlength(); log.e("lenghtoffile "," = " + lenghtoffile); // input stream read file - 8k buffer if(lenghtoffile > 0) { inputstream input = new bufferedinputstream(url.openstream(),10*1024); // output stream write file in sd card newfolder = new file(environment.getexternalstoragedirectory().getpath() + file.separator + "classnkk_audio"); file file = new file(newfolder, strimagename); outputstream output = new fileoutputstream(file); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; publishprogress("" + (int) ((total * 100) / lenghtoffile)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); log.e("audio files", "download ans save in sd card !!!"); log.e("======================"," downloadmusicfrominternet ======================"); } } catch (exception e) { log.e("error: ", e.getmessage()); } return null; } }.execute(); }
and here image download code
void download_pngfile(string fileurl, string imagename) { try { url imgurl = new url(fileurl); httpurlconnection conn = (httpurlconnection) imgurl.openconnection(); conn.connect(); int lenghtofimage_file = conn.getcontentlength(); log.e("lenghtofimage_file "," = "+lenghtofimage_file); if(lenghtofimage_file > 0) { bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 1; bitmap imagenobtenida = bitmapfactory.decodestream(conn.getinputstream(), null, options); file file = new file(newfolder, imagename); if (file.exists()) file.delete(); try { fileoutputstream out = new fileoutputstream(file); imagenobtenida.compress(bitmap.compressformat.png, 100, out); out.flush(); out.close(); int imagenobtenidaw = imagenobtenida.getwidth(); int imagenobtenidah = imagenobtenida.getheight(); log.e("imagenobtenidaw " ," = +" + imagenobtenidaw + " imagenobtenidah = " + imagenobtenidah); } catch (exception e) { } } } catch (ioexception e) { e.printstacktrace(); } } if (imagename.endswith(mp3_pattern)) { str_downloadurl = namespace + "/downloadfile/filename/"+imagename; downloadaudiofile(str_downloadurl ,imagename); strdownloadstatus = "1"; dbhelper.update_downloadstatus(imagename, strdownloadstatus); } if (imagename.endswith(png_pattern) || imagename.endswith(jpg_pattern) || imagename.endswith(bmp_pattern) || imagename.endswith(gif_pattern) || imagename.endswith(jpeg_pattern)) { str_downloadurl = namespace + "/downloadfile/filename/" + imagename; download_pngfile(str_downloadurl,imagename); strdownloadstatus = "1"; dbhelper.update_downloadstatus(imagename, strdownloadstatus); }
Comments
Post a Comment