Bonjour
Je suis entrain de developper une application android similaire a google drive une parmi ses fonctionnalites est de telecharger un fichier du serveur vers mon smartphone.Pour ce faire jai utilise les fonctions suivantes que je l ai pris d un projet exsistant sur github
Method for the download notifier :
et pour la classe du telechargemet :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public void notificationDownload() { isDownload = false; try { notificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE); String MyText = "Telechargement"; titleText = URLDecoder.decode(fileName); notiText = "Téléchargement en cours..."; Log.d("Download file name ",fileName + " <======="); notification = new Notification(R.drawable.icon, MyText, System.currentTimeMillis()); Intent MyIntent = new Intent(Intent.ACTION_VIEW); MyIntent.putExtra("extendedTitle", titleText); PendingIntent StartIntent = PendingIntent.getActivity( getApplicationContext(), 0, MyIntent, 0); notification.setLatestEventInfo(getApplicationContext(), titleText, notiText, StartIntent); notificationManager.notify(NOTFICATION_ID, notification); Intent i = new Intent(getApplicationContext(), Downloader.class); Log.v(TAG, "you are entring to download a file"); i.setData(Uri.parse(mFileUrl)); Log.v(TAG, "step1"); i.putExtra(Downloader.EXTRA_MESSENGER, new Messenger(notiHandler)); Log.v(TAG, "step2"); startService(i); Log.v(TAG, "step3"); } catch (Exception e){ Log.v(TAG, "probleme de telechargement://"+e); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 public class Downloader extends IntentService { public static final String EXTRA_MESSENGER="com.cloud.downloadmanager.downloader.EXTRA_MESSENGER"; private HttpClient client=null; Intent i; public Downloader() { super("Downloader"); } @Override public void onCreate() { super.onCreate(); client=new DefaultHttpClient(); } @Override public void onDestroy() { super.onDestroy(); client.getConnectionManager().shutdown(); } @Override public void onHandleIntent(Intent i) { downloadFile(i); } Thread download = new Thread() { @Override public void run() { for (int i = 1; i < 100; i++) { DashBoardActivity.updateNotation(); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Bundle extras=i.getExtras(); if (extras!=null) { Messenger messenger=(Messenger)extras.get(EXTRA_MESSENGER); Message msg=Message.obtain(); msg.arg1=Activity.RESULT_OK; try { messenger.send(msg); } catch (android.os.RemoteException e1) { Log.w(getClass().getName(), "Exception sending message", e1); } } } }; public void downloadFile(Intent i) { if(isOnline()) { String url = i.getData().toString(); GetMethod gm = new GetMethod(url); try { int status = BaseActivity.httpClient.executeMethod(gm); // Toast.makeText(getApplicationContext(), String.valueOf(status),2).show(); Log.e("HttpStatus","==============>"+String.valueOf(status)); if (status == HttpStatus.SC_OK) { InputStream input = gm.getResponseBodyAsStream(); String fileName = new StringBuffer(url).reverse().toString(); String[] fileNameArray = fileName.split("/"); fileName = new StringBuffer(fileNameArray[0]).reverse() .toString(); fileName = URLDecoder.decode(fileName); File folder = new File( BaseActivity.mDownloadDest); boolean success = false; if (!folder.exists()) { success = folder.mkdir(); } if (!success) { // Do something on success File file = new File(BaseActivity.mDownloadDest, fileName); OutputStream fOut = new FileOutputStream(file); int byteCount = 0; byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = input.read(buffer)) != -1) { fOut.write(buffer, 0, bytesRead); byteCount += bytesRead; // DashBoardActivity.updateNotation(); } fOut.flush(); fOut.close(); } else { // Do something else on failure Log.d("Download Prob..", String.valueOf(success) + ", Some problem in folder creating"); } } } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } gm.releaseConnection(); Bundle extras=i.getExtras(); if (extras!=null) { Messenger messenger=(Messenger)extras.get(EXTRA_MESSENGER); Message msg=Message.obtain(); msg.arg1=Activity.RESULT_OK; try { messenger.send(msg); } catch (android.os.RemoteException e1) { Log.w(getClass().getName(), "Exception sending message", e1); } } } else { WebNetworkAlert(); } } public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { // Try This int i = netInfo.getType(); System.out.println("Net Type =" + i); return true; } return false; } public void WebNetworkAlert() { new AlertDialog.Builder(this).setTitle("Network Error") .setMessage("Internet connection not available.") .setPositiveButton("Ok", new OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // do stuff onclick of YES // finish(); return; } }).show(); } }
Partager