知道路徑就可以得到流, 得到流就可以得到bitmap imageview.setImageBitmap(bitmap);
ImageView是Android程序中經常用到的組件,它將一個圖片顯示到屏幕上。
在UI xml定義一個ImageView如下:
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.myimage);
- ImageView image1 = (ImageView) findViewById(R.myImage.image);
- //Bitmap bitmap = getLoacalBitmap(“/aa/aa.jpg”); //從本地取圖片
- Bitmap bitmap = getHttpBitmap(“http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg”); //從網上取圖片
- image1 .setImageBitmap(bitmap); //設置Bitmap
- }
- /**
- * 加載本地圖片
- * http://bbs.3gstdy.com
- * @param url
- * @return
- */
- public static Bitmap getLoacalBitmap(String url) {
- try {
- FileInputStream fis = new FileInputStream(url);
- return BitmapFactory.decodeStream(fis);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 從服務器取圖片
- *http://bbs.3gstdy.com
- * @param url
- * @return
- */
- public static Bitmap getHttpBitmap(String url) {
- URL myFileUrl = null;
- Bitmap bitmap = null;
- try {
- Log.d(TAG, url);
- myFileUrl = new URL(url);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- try {
- HttpURLConnection conn = (HttpURLConnection) myFileUrl
- .openConnection();
- conn.setConnectTimeout(0);
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- bitmap = BitmapFactory.decodeStream(is);
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return bitmap;
- }