Senin, 23 Juli 2012

Menampilkan Image dari Web pada Android (Pemrograman Android)

Di bawah ini adalah Project Android untuk menampilkan Gambar / Image dari web ke dalam komponen ImageView pada Android

Langkahnya, buat Project Android baru di Eclipse.

Pada desain tampilan main.xml tambahkan satu buah komponen ImageView, sehingga menjadi seperti di bawah ini :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>


Kemudian tambahkan kode program berikut pada MainActivity.java :




package com.amijaya.viewimageweb;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

//http://cariprogram.blogspot.com
//nuramijaya@gmail.com

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView iViewer = (ImageView)findViewById(R.id.ImageView01);

/*String myJpgPath = "/sdcard/Foto/Candi/Borobudur.jpg"; 

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options);
    iViewer.setImageBitmap(bm); */

BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = LoadImage("http://www.android.com/images/logo.png", bmOptions);
iViewer.setImageBitmap(bm);
}

private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{       
Bitmap bitmap = null;
InputStream in = null;       
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;               
}

private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();

try{
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();

if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}
catch (Exception ex)
{
}
return inputStream;
}
}


Jangan lupa untuk menambahkan permission Internet agar dapat terhubung ke Jaringan Internet pada AndroidManifest.xml :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.amijaya.viewimageweb"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest> 


Hasilnya :



Project lengkap dapat diunduh pada link ini. Setelah sampai di Google Docs, klik File - Download.

Semoga bermanfaat.

Feedback : nuramijaya@gmail.com