zoukankan      html  css  js  c++  java
  • 本地图片选择(打开媒体库,选择图片)

    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(){  
                @Override  
                public 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);  
                }  
                  
            });  
        }  
          
        @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 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);  
        }  
    }  
  • 相关阅读:
    C语言memmove()函数: 复制内存内容(可以重叠的内存块)
    #pragma pack(n)
    c++中sizeof的理解
    c/c++gdb下和发布版本下输出地址不同
    LeetCode 141. Linked List Cycle
    LeetCode 143. Reorder List
    TCP简介(一)
    通过cat方式生成yum源
    手动添加ceph的mds
    Linux 配置nfs
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5209112.html
Copyright © 2011-2022 走看看