Android: ImageView: select a local image and display it
Running result:
Three simulator libraries are useless ~ Image quality is quite touching ~
An implicit intent
Layout file:
<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"></imageview></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;@Overrideprotected 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);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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;}}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubif (requestCode == 0x1 && resultCode == RESULT_OK) {if (data != null) {image_choose.setImageURI(data.getData());}}super.onActivityResult(requestCode, resultCode, data);}}