Sunday, April 8, 2012

Load ImageView with JPG file in SD Card


Load ImageView with JPG file in SD Card

android.graphics.BitmapFactory class provide a method decodeFile(String pathName) to decode a file path into a bitmap. The decoded bitmap can be loaded to ImageView using setImageBitmap() method of the ImageView.

Load ImageView with JPG file in SD Card

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.AndroidLoadImageView;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
 
public class AndroidLoadImageViewActivity extends Activity {
  
 String imagefile ="/sdcard/IMG_9331.JPG";
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        ImageView image = (ImageView)findViewById(R.id.image);
        Bitmap bm = BitmapFactory.decodeFile(imagefile);
        image.setImageBitmap(bm);
    }
}


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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/image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>