Media Store Gallery
package app.test;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class Test extends Activity {
public final static int DISPLAYWIDTH = 200;
public final static int DISPLAYHEIGHT = 200;
TextView titleTextView;
ImageButton imageButton;
Cursor cursor;
Bitmap bmp;
String imageFilePath;
int fileColumn;
int titleColumn;
int displayColumn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
titleTextView = (TextView) this.findViewById(R.id.TitleTextView);
imageButton = (ImageButton) this.findViewById(R.id.ImageButton);
String[] columns = { Media.DATA, Media._ID, Media.TITLE,
Media.DISPLAY_NAME };
cursor = managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null,
null);
fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
titleColumn = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE);
displayColumn = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
if (cursor.moveToFirst()) {
titleTextView.setText(cursor.getString(displayColumn));
imageFilePath = cursor.getString(fileColumn);
bmp = getBitmap(imageFilePath);
imageButton.setImageBitmap(bmp);
}
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (cursor.moveToNext()) {
titleTextView.setText(cursor.getString(displayColumn));
imageFilePath = cursor.getString(fileColumn);
bmp = getBitmap(imageFilePath);
imageButton.setImageBitmap(bmp);
}
}
});
}
private Bitmap getBitmap(String imageFilePath) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
bmpFactoryOptions.inSampleSize = 2;
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
return bmp;
}
}
//layout/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"
>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageButton"></ImageButton>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/TitleTextView"
android:text="Image Title"/>
</LinearLayout>
Related examples in the same category