• 从Gallery中获取图片示例


    一、MainActivity类

    package com.example.gallerydemo;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.View;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
        private ImageView iv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv=(ImageView) findViewById(R.id.iv);
        }
    
        public void selectImage(View view) {
            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, 0);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            if (data != null) {
                Uri uri=data.getData();//图片的uri路径
                iv.setImageURI(uri);
                //缩略图 Bitmap bitmap=data.getParcelableExtra("data");
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
        
    }
    View Code
    二、layout/activity_main.xml
    <LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity" >
        <Button 
            android:onClick="selectImage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="显示一张图片"
            />
    
        <ImageView
            android:id="@+id/iv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </LinearLayout>
    View Code
  • 相关阅读:
    LOJ DFS序
    牛客练习赛31 D神器大师泰兹瑞与威穆
    Codeforces Round #487 (Div. 2) C
    Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C D
    [Windows Server 2003] 还原SQL Server数据库
    [Windows Server 2008] 查看PHP详细错误信息
    [Windows Server 2008] 安装网站伪静态
    网站Gzip压缩
    [Windows Server 2008] SQL Server 2008 数据库还原方法
    [Windows Server 2008] 安装Apache+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/hyzhou/p/3446127.html
Copyright © 2020-2023  润新知