Android 利用Service实现下载网络图片至sdk卡

时间:2023-03-08 18:50:57
 package com.example.myapp5;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Android 利用service实现下载图片功能
* @author shaobn
* @date 2015-9-14
* @packege com.example.myapp5.MyApp5
*/
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
});
}
}
 package com.example.myapp5;

 import java.io.File;
import java.io.FileOutputStream; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast; public class MyService extends Service {
private final static String IMAGEPAH = "https://www.baidu.com/img/bd_logo1.png";
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
final Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) {
if(msg.what==1){
Toast.makeText(getApplicationContext(), "关闭了", 1).show();
stopSelf();
}
}; };
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(IMAGEPAH);
HttpResponse httpResponse = null;
File file = Environment.getExternalStorageDirectory();
File file2 = null;
FileOutputStream fos = null;
try {
httpResponse =httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode()==200) {
byte[] num = EntityUtils.toByteArray(httpResponse.getEntity());
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
file2 = new File(file, "/Movies/a.jpg");
file2.createNewFile();
fos = new FileOutputStream(file2);
fos.write(num);
Message message = Message.obtain();
message.what = 1;
handler.sendMessage(message);
}
} } catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}finally{
if(fos!=null){
try {
fos.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
} } }
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}