public class GetPictureFromInternetActivity extends Activity
{
private ImageView imageView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url = "http://img1.gcimg.net/att/day_120330/1203301402671605a8a7994804.png";
// String url = "http://www.gezila.com/uploads/allimg/110110/1_110110084544_1.jpg";
imageView = (ImageView) this.findViewById(R.id.imageView);
Bitmap bitmap = getHttpBitmap(url);//从网络获取图片
imageView.setImageBitmap(bitmap);
savePicture(bitmap);//保存图片到SD卡
}
public Bitmap getHttpBitmap(String url)
{
Bitmap bitmap = null;
try
{
URL pictureUrl = new URL(url);
InputStream in = pictureUrl.openStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return bitmap;
}
public void savePicture(Bitmap bitmap)
{
String pictureName = "/mnt/sdcard/" + "car"+".jpg";
File file = new File(pictureName);
FileOutputStream out;
try
{
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Exit");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
System.exit(0);
return true;
}
});
return true;
}
}
public Bitmap getHttpBitmap(String url)
{
Bitmap bitmap = null;
try
{
URL pictureUrl = new URL(url);
InputStream in = pictureUrl.openStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return bitmap;
}
public Bitmap getHttpBitmap(String url)
{
Bitmap bitmap = null;
try
{
URL pictureUrl = new URL(url);
URLConnection con = pictureUrl.openConnection();
InputStream in = con.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return bitmap;
}
public Bitmap getHttpBitmap(String url)
{
Bitmap bitmap = null;
try
{
URL pictureUrl = new URL(url);
HttpURLConnection con = (HttpURLConnection) pictureUrl.openConnection();
InputStream in = con.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return bitmap;
}
public void savePicture(Bitmap bitmap)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
try
{
File sdcardDir = Environment
.getExternalStorageDirectory();
Time t = new Time();
t.setToNow();
String filename = sdcardDir.getCanonicalPath()
+ "/DCIM/camera"
+ String.format(
"/ReeCamdddddd.jpg",
t.year, t.month + 1, t.monthDay,
t.hour, t.minute, t.second);
File file = new File(filename);
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有