来自网址的Android图片视图

前端之家收集整理的这篇文章主要介绍了来自网址的Android图片视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从URL下载图像,但下载完成后图像不会更改.
我在下面输入代码,有没有人经历过同样的事情?

Java文件

  1. public class MyImgActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. ImageView imgView =(ImageView)findViewById(R.id.imageView1);
  8. Drawable drawable = LoadImageFromWebOperations("http://www.gophoto.it/view.PHP?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
  9.  
  10. imgView.setImageDrawable(drawable);
  11. }
  12. private Drawable LoadImageFromWebOperations(String url) {
  13. try
  14. {
  15. InputStream is = (InputStream) new URL(url).getContent();
  16. Drawable d = Drawable.createFromStream(is,"src name");
  17. return d;
  18. }catch (Exception e) {
  19. System.out.println("Exc="+e);
  20. return null;
  21. }
  22. }
  23. }

XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6.  
  7. <ImageView
  8. android:id="@+id/imageView1"
  9. android:layout_height="match_parent"
  10. android:layout_width="match_parent"></ImageView>
  11. </LinearLayout>

清单文件

  1. <uses-permission android:name="android.permission.INTERNET"/>

解决方法

请使用以下代码下载并将图像显示到imageview.
  1. public class image extends Activity {
  2. /** Called when the activity is first created. */
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7.  
  8. Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.PHP?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
  9. ImageView img = (ImageView) findViewById(R.id.img);
  10. img.setImageBitmap(bitmap);
  11. }
  12.  
  13. private InputStream OpenHttpConnection(String urlString) throws IOException {
  14. InputStream in = null;
  15. int response = -1;
  16.  
  17. URL url = new URL(urlString);
  18. URLConnection conn = url.openConnection();
  19.  
  20. if (!(conn instanceof HttpURLConnection))
  21. throw new IOException("Not an HTTP connection");
  22.  
  23. try {
  24. HttpURLConnection httpConn = (HttpURLConnection) conn;
  25. httpConn.setAllowUserInteraction(false);
  26. httpConn.setInstanceFollowRedirects(true);
  27. httpConn.setRequestMethod("GET");
  28. httpConn.connect();
  29. response = httpConn.getResponseCode();
  30. if (response == HttpURLConnection.HTTP_OK) {
  31. in = httpConn.getInputStream();
  32. }
  33. } catch (Exception ex) {
  34. throw new IOException("Error connecting");
  35. }
  36. return in;
  37. }
  38.  
  39. private Bitmap DownloadImage(String URL) {
  40. Bitmap bitmap = null;
  41. InputStream in = null;
  42. try {
  43. in = OpenHttpConnection(URL);
  44. bitmap = BitmapFactory.decodeStream(in);
  45. in.close();
  46. } catch (IOException e1) {
  47. // TODO Auto-generated catch block
  48. e1.printStackTrace();
  49. }
  50. return bitmap;
  51. }
  52. }

猜你在找的Android相关文章