Jun 13, 2011

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

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);
}
}


<?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>



Related:
- ImageView
- Reduce Bitmap size using BitmapFactory.Options.inSampleSize
- Read Exif of JPG file using ExifInterface

1 comment:

Infolinks In Text Ads