1.先写一个工具类
public class ImgDonwload {
private static String filePath;
private static Bitmap mBitmap;
private static String mFileName="为米老乡";
private static String mSaveMessage;
private final static String TAG = "ImageActivity";
private static Context context;
private static ProgressDialog mSaveDialog = null;
public static void donwloadImg(Context contexts,String filePaths){
context = contexts;
filePath = filePaths;
mSaveDialog = ProgressDialog.show(context, "保存图片", "图片正在保存中,请稍等...", true);
new Thread(saveFileRunnable).start();
}
private static Runnable saveFileRunnable = new Runnable(){
@Override
public void run() {
try {
byte[] data = getImage(filePath);
if(data!=null){
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// bitmap
}else{
Toast.makeText(context, "Image error!", Toast.LENGTH_LONG).show();
}
saveFile(mBitmap, mFileName);
mSaveMessage = "图片保存成功!";
} catch (IOException e) {
mSaveMessage = "图片保存失败!";
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
messageHandler.sendMessage(messageHandler.obtainMessage());
}
};
private static Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mSaveDialog.dismiss();
Toast.makeText(context, mSaveMessage, Toast.LENGTH_SHORT).show();
}
};
/**
* Get image from newwork
* @param path The path of image
* @return byte[]
* @throws Exception
*/
public static byte[] getImage(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
return readStream(inStream);
}
return null;
}
/**
* Get data from stream
* @param inStream
* @return byte[]
* @throws Exception
*/
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
/**
* 保存文件
* @param bm
* @param fileName
* @throws IOException
*/
public static void saveFile(Bitmap bm, String fileName) throws IOException {
File dirFile = new File(Environment.getExternalStorageDirectory().getPath());
if(!dirFile.exists()){
dirFile.mkdir();
}
fileName = UUID.randomUUID().toString()+".jpg";
File jia=new File(Environment.getExternalStorageDirectory().getPath() +"/DCIM/酷炫");
if(!jia.exists()){ //判断文件夹是否存在,不存在则创建
jia.mkdirs();
}
File myCaptureFile = new File(jia +"/"+ fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
//把图片保存后声明这个广播事件通知系统相册有新图片到来
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(myCaptureFile);
intent.setData(uri);
context.sendBroadcast(intent);
}
2.直接调用即可
ImgDonwload.donwloadImg(MainActivity.this,url); //url为所要下载的图片地址
非常简单,适合大家通用
当然,如果要显示本地的图片,我们只要知道他的路径即可
String img_path = Environment.getExternalStorageDirectory().getPath() +"/DCIM/酷炫/xiaoqingnian.jpg"; Bitmap bmp= BitmapFactory.decodeFile(img_path); img.setImageBitmap(bmp);