Gallery的使用(二):显示抓拍的图片

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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 MainActivity 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;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		titleTextView = (TextView)findViewById(R.id.TitleTextView);
		imageButton = (ImageButton)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;
	}
}
	
	

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值