码迷,mamicode.com
首页 > 移动开发 > 详细

android:ImageView选择本地图片并显示

时间:2016-03-03 21:27:13      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

运行结果:

技术分享

模拟器图库就三张 没办法~画质挺感人~

一个隐式意图

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image_choose"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:background="#CC0000" 
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:src="@drawable/default_bg"
        android:padding="5dp"
        />

</RelativeLayout>


MainActivity.class

package com.example.chooseimage;

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

	private ImageView image_choose;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		image_choose = (ImageView) findViewById(R.id.image_choose);
		image_choose.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.image_choose: {
			Intent intent = new Intent(Intent.ACTION_PICK, null);
			intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					"image/*");
			startActivityForResult(intent, 0x1);
			break;
		}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 0x1 && resultCode == RESULT_OK) {
			if (data != null) {
				image_choose.setImageURI(data.getData());
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}


android:ImageView选择本地图片并显示

标签:

原文地址:http://blog.csdn.net/su20145104009/article/details/50790983

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!