2014-10-19 102 views
-1

我只是寫了這個代碼:如何顯示從android下載並顯示一個Bitamp圖像?

public class MyActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    String latEiffelTower = "48.858235"; 
    String lngEiffelTower = "2.294571"; 
    String url = "http://maps.google.com/maps/api/staticmap?center=" + latEiffelTower + "," + lngEiffelTower + "&zoom=15&size=200x200&sensor=false"; 
    try { 
     ImageView i = (ImageView)findViewById(R.id.image); 
     Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent()); 
     i.setImageBitmap(bitmap); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

的話,我想下載從谷歌靜態地圖靜態圖像位圖和森上ImageView的,但我的程序停止當我運行它,怎麼讓這個位圖圖像在這段代碼中?我是否使用過任何特殊許可或其他任何東西?

,這是我的main.xml

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MyActivity"> 

<TextView 
    android:text="@string/hello_world" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/image"/> 

</RelativeLayout> 

回答

0

使用通用圖像裝載機模塊。它可以在https://github.com/nostra13/Android-Universal-Image-Loader

public class WebImageLoader { 
    DisplayImageOptions options; 
    ImageLoader imageLoader; 

    public WebImageLoader(Context context) { 

     options = new DisplayImageOptions.Builder() 
       .showImageOnLoading(context.getResources().getDrawable(R.drawable.ic_stub)) 
       .showImageForEmptyUri(context.getResources().getDrawable(R.drawable.ic_empty)) 
       .showImageOnFail(context.getResources().getDrawable(R.drawable.ic_error)) 
       .cacheInMemory(false) 
       .cacheOnDisk(true) 
       .considerExifParams(true) 
       .build(); 

     imageLoader = ImageLoader.getInstance(); 
     if (!imageLoader.isInited()) { 
      imageLoader.init(ImageLoaderConfiguration.createDefault(context)); 
     } 
    } 

    public void displayImage(ImageView imageView,String imageURL) { 
     try { 
      imageLoader.displayImage(imageURL, imageView, options); 
     } 
     catch (Exception e) { 
      //Handle the exception 
     } 
    } 
} 

中找到爲了使用它

WebImageLoader imageLoader = new WebImageLoader(context); 
ImageView imgProductImage = (ImageView) findViewById(R.id.imgProductImage); 
imageLoader.displayImage(imgProductImage,url); 

感謝, Noorul

0

我會建議使用volley,程序停止,因爲你不能運行在Android主線程網絡代碼因爲它保留爲UI任務

-1

您可以簡單地使用Picasso下載圖像即:

String latEiffelTower = "48.858235"; 
String lngEiffelTower = "2.294571"; 
String url = "http://maps.google.com/maps/api/staticmap?center=" + latEiffelTower + "," + lngEiffelTower + "&zoom=15&size=200x200&sensor=false"; 
ImageView i = (ImageView)findViewById(R.id.image); 

Picasso.with(MainActivity.this).load(url).into(i); 

欲瞭解更多信息,請訪問Picasso

+1

它沒有工作:) – 2014-10-21 09:27:56

相關問題