// 读取SD卡图片
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file = Environment.getExternalStorageDirectory();
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath()+"/miq/head_1001_5.png");
}
//Bitmap转Drawable
Bitmap bitmap = new Bitmap (...);
Drawable drawable = new BitmapDrawable(bitmap );
//网络下载图片保存到SD卡
Button button = (Button) this.findViewById(R.id.button);
String downloadUrl = "http://192.168.1.7:8077/iphone4/head_1001_5.png";
URL url;
try {
url = new URL(downloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Referer", downloadUrl);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
if (conn.getResponseCode()==200) {
int fileSize = conn.getContentLength();//根据响应获取文件大小
if (fileSize <= 0) throw new RuntimeException("Unkown file size ");
String filename = "head_1001_5.png";//获取文件名称
File file = Environment.getExternalStorageDirectory();
file = new File(file.getPath()+"/miq");
if (!file.exists()) {
file.mkdirs();
}
File saveFile = new File(file, filename);//构建保存文件
RandomAccessFile threadfile = new RandomAccessFile(saveFile, "rwd");
InputStream inStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int offset = 0;
while ((offset = inStream.read(buffer, 0, 1024)) != -1) {
threadfile.write(buffer, 0, offset);
}
threadfile.close();
inStream.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}