读书人

当地片选择

发布时间: 2012-08-29 08:40:14 作者: rapoo

本地片选择

package com.yfz;import java.io.FileNotFoundException;import android.app.Activity;import android.content.ContentResolver;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class Lesson_01_Pic extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Button button = (Button)findViewById(R.id.b01);        button.setText("选择图片");        button.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v) { Intent intent = new Intent();        /* 开启Pictures画面Type设定为image */        intent.setType("image/*");        /* 使用Intent.ACTION_GET_CONTENT这个Action */        intent.setAction(Intent.ACTION_GET_CONTENT);         /* 取得相片后返回本画面 */        startActivityForResult(intent, 1);}                });    }    @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == RESULT_OK) {Uri uri = data.getData();Log.e("uri", uri.toString());ContentResolver cr = this.getContentResolver();try {Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));ImageView imageView = (ImageView) findViewById(R.id.iv01);/* 将Bitmap设定到ImageView */imageView.setImageBitmap(bitmap);} catch (FileNotFoundException e) {Log.e("Exception", e.getMessage(),e);}}super.onActivityResult(requestCode, resultCode, data);}}

读书人网 >移动开发

热点推荐