Wednesday, July 14, 2010

Load bitmap file from SD Card

In last exercise "Save file to SD Card", a file have been saved in SD Card. In this exercise, the bitmap file in SD Card will be loaded, using BitmapFactory.

Load bitmap file from SD Card

main.xml
<?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/imageview"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
</LinearLayout>


AndroidBitmap.java
package com.exercise.AndroidBitmap;

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

public class AndroidBitmap extends Activity {

private final String imageInSD = "/sdcard/er.PNG";

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

Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
myImageView.setImageBitmap(bitmap);

}
}


Download the files.

next: Scale bitmap image, using Matrix

7 comments:

Victoria said...

Hi there,


sorry, to bother again, that tutorial looks great because it contains what I actually need, but my problem that I don't know how to retrieve or access all files available on the SD card and display them in the grid view. If someone could help me out here, that would be great

Victoria said...

Sorry, forgot to mention that I also want to load videos :)

Android Er said...

Do you need a file explorer? may be you can reference Implement a simple File Explorer in Android

For video, I have tried using VideoView only, Displays video in VideoView

Ventura said...

ok, I know this works because you've got the proper ImageView declared in the XML file, but how would you go about it if you obtained the image paths dynamically, and didn't have the XML views declared?

Android Er said...

hello Ventura,

For "ImageView declared in the XML", it simple allocate a view to display image, not related to where the image come from. I can't catch what you means "didn't have the XML views declared". Do you means your layout XML have no ImageView declared?

For "obtained the image paths dynamically", please refer Display Gallery selected image using BitmapFactory.

Ventura said...

Sorry, I think I didn't express myself very clearly, but thanks for the prompt response. What I meant is:
I perform a search on a database, which returns the paths (/sdcard/whatever) to the images. The catch is you can't know "a priori" how many images will be returned (could be anywhere from 0 to 1000), and therefore you can't declare the ImageViews in the XML file, you have to declare them, as needed, in the java file.
I think I solved it, though, by inserting these ImageView into a Gallery (which, as you only need one, can be declared in the XML)

Android Er said...

hello Ventura,

Yes, I agree that gallery is a good choice for your case.