android 工程,点击图片,在屏幕显示

1:首先创建一个android 工程名字自定义(此处为MyTest)
2:替换MainActivity.java 代码如下

package com.example.mytest;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    Button mButton;
    ImageView mImageView;
    TextView mTextView;
    Bitmap bm;
    private static final int REQUEST_CODE_SELECT_PIC = 120;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //注册三个组件
        mButton = (Button) findViewById(R.id.button1);
        mImageView = (ImageView) findViewById(R.id.imageView1);
        mTextView = (TextView) findViewById(R.id.textView1);

        mButton.setOnClickListener(this);

    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(arg0.getId() == R.id.button1)
        {
            selectPicture();
        }


    }

    private void selectPicture() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setType("image/*");
        // 这个函数会自动调用下面的函数
        startActivityForResult(intent, REQUEST_CODE_SELECT_PIC);
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {



        if (requestCode == REQUEST_CODE_SELECT_PIC) {

            // 获取选择的图片
            Uri selectedImage = data.getData();
            int width = 0;
            int height = 0;

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage, null,
                    null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            // 获取到图片的路径
            String selectedImagePath = cursor.getString(columnIndex);


            Log.i("zhang selectedImagePath", selectedImagePath);
            // 调取底层处理
            //DealImage(selectedImagePath);
            // 解码该路径的图片,得到bitmap 图片

            bm = BitmapFactory.decodeFile(selectedImagePath);

            width = bm.getWidth();
            height = bm.getHeight();

            if (selectedImage != null) 
            {
                //将图片显示到ImageView中
                mImageView.setImageBitmap(bm);

            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
3: 替换res 下的layout 文件夹下面的 activity_main.xml ,代码如下
"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    "@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="TextView" />



    "@+id/imageView1"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_x="116dp"
        android:layout_y="26dp"
        android:src="@drawable/ic_launcher" />


    

你可能感兴趣的:(android)