显示本地图片整理

package com.example.ws;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {

	private EditText edit;
	private Button btn;
	Bitmap bmp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		edit = (EditText) this.findViewById(R.id.edit);
		btn = (Button) this.findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("fdsfsdf");
				
				Intent picture = new Intent(Intent.ACTION_GET_CONTENT);
				picture.setType("image/*");
				picture.addCategory(Intent.CATEGORY_OPENABLE);
				startActivityForResult(Intent.createChooser(picture, "选择图片"), 0);

			}
		});

	}

	public void srcrem(Bitmap bmps) {

		Drawable drawable = new BitmapDrawable(bmps);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		String str = "0";
		SpannableString spannable = new SpannableString(str);
		ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
		spannable.setSpan(span, 0, 0, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		edit.setText(spannable);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			Uri uri = data.getData();
			String[] proj = { MediaStore.Images.Media.DATA };
			Cursor cursor = managedQuery(uri, proj, // Which columns to return
					null, // WHERE clause; which rows to return (all rows)
					null, // WHERE clause selection arguments (none)
					null); // Order-by clause (ascending by name)

			int column_index = cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();

			String path = cursor.getString(column_index);
			bmp = BitmapFactory.decodeFile(path);
			System.out.println("the path is :" + path);
			srcrem(bmp);
		} else {
			Toast.makeText(Main.this, "请重新选择图片", Toast.LENGTH_SHORT).show();
		}
	}

}

你可能感兴趣的:(显示本地图片整理)