android 选择本地图片并预览

adv_sdcard_image_upload.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:weightSum="1">
    <TextView android:id="@+id/txtv_title" android:text="本地400电话 图片添加" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:gravity="center" android:layout_weight="0.29">
        <Button android:text="选择" android:id="@+id/adv_btn_xuanze"  android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
       
        <Button android:text="提交" android:id="@+id/adv_btn_tijiao" android:layout_marginLeft="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
         <Button android:text="返回" android:id="@+id/adv_btn_fanhui" android:layout_marginLeft="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_weight="0.22">
        <TextView android:text="任务名称:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <EditText android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/editText1">
            <requestFocus></requestFocus>
        </EditText>
    </LinearLayout>
    <ImageView android:layout_gravity="center_horizontal" android:src="@drawable/clickbutttonstyle"
    android:id="@+id/adv_img_show" android:layout_width="302dp" android:layout_height="284dp"></ImageView>
   
</LinearLayout>

adv_sdcard_image_upload.java

package adv.activity;

import java.io.FileNotFoundException;

import frame.hrxcCall.R;
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;
import android.widget.Toast;

public class adv_sdcard_image_upload extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.adv_sdcard_image_upload);
        init();

    }

    /**
     * 初始化方法 http://www.my400800.cn
     */
    private void init() {
        // 事件注册start
        // 选择
        Button adv_btn_xuanze = (Button) findViewById(R.id.adv_btn_xuanze);
        // 提交
        Button adv_btn_tijiao = (Button) findViewById(R.id.adv_btn_tijiao);
        // 返回
        Button adv_btn_fanhui = (Button) findViewById(R.id.adv_btn_fanhui);

        // 返回按钮按下处理事件
        adv_btn_fanhui.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();// 这个是关键

            }
        });

        // 事件注册end
        adv_btn_xuanze.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                /* 开启Pictures画面Type设定为image */
                intent.setType("image/*");
                /* 使用Intent.ACTION_GET_CONTENT这个Action */
                intent.setAction(Intent.ACTION_GET_CONTENT);
                /* 取得相片后返回本画面 */
                startActivityForResult(intent, 1);

            }
        });

        // /* 开启Pictures画面Type设定为image */
        // intent.setType("image/*");
        // /* 使用Intent.ACTION_GET_CONTENT这个Action */
        // intent.setAction(Intent.ACTION_GET_CONTENT);
        // /* 取得相片后返回本画面 */
        // startActivityForResult(intent, 1);
    }
   
    /**
     * 文件选取完成回调函数
     */
    @Override
    protected 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 adv_img_show = (ImageView) findViewById(R.id.adv_img_show);
                /* 将Bitmap设定到ImageView */
                adv_img_show.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                Log.e("Exception", e.getMessage(),e);
                Toast.makeText(getApplicationContext(), "选择文件没有发现", Toast.LENGTH_LONG).show();
            }
        }
        super.onActivityResult(requestCode, resultCode, data); 
    }

}

效果:

原文地址:https://www.cnblogs.com/jishu/p/2195500.html