Linux教程網
向ImageManage中傳一個圖片url
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
-
- import Android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.util.Log;
-
- public class ImageManage
- {
- public static Bitmap getImage(String url)
- {
- URL imageUrl = null;
- Bitmap bitmap = null;
-
- try
- {
- imageUrl = new URL(url);
- } catch (MalformedURLException e)
- {
- Log.e("url error", "url======:"+url);
- }
- try
- {
- HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- BufferedInputStream bis = new BufferedInputStream(is);
- bitmap = BitmapFactory.decodeStream(bis);
- bis.close();
- is.close();
- } catch (IOException e)
- {
- e.printStackTrace();
- Log.e("connect error", "連接失敗");
- }
-
- return bitmap;
- }
-
- }
Copyright ©
Linux教程網 All Rights Reserved