Feb 4, 2014

Load image from resource ID using ImageManager

com.google.android.gms.common.images.ImageManager is a class in Google Play Services API, used to load images from the network and handles local caching. This example load images from resource ID using ImageManager.

Load image from resource ID using ImageManager
Load image from resource ID using ImageManager
- Before use Google Play Service API, you have to Set Up Google Play Services SDK.

-  Referencing a Google Play Service Library Project in your project, read here.

- Add <meta-data> of Google Play services library as a dependency as a child of the <application> in AndroidManifest.xml.
        <meta-data android:name="com.google.android.gms.version" 
            android:value="@integer/google_play_services_version" />

Example code:

- MainActivity.java
package com.example.androidloadimage;

import com.google.android.gms.common.images.ImageManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ImageView image1 = (ImageView)findViewById(R.id.image1);
  ImageView image2 = (ImageView)findViewById(R.id.image2);
  ImageView image3 = (ImageView)findViewById(R.id.image3);
  
  ImageManager imageManager = ImageManager.create(MainActivity.this);
  imageManager.loadImage(image1, R.drawable.ic_launcher);
  imageManager.loadImage(image2, android.R.drawable.ic_dialog_map);
  imageManager.loadImage(image3, R.drawable.common_signin_btn_icon_light);

 }

}

- activity_main.xml
<LinearLayout 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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/image3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

No comments:

Post a Comment

Infolinks In Text Ads