源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android中ImageView实现选择本地图片并显示功能

  • 时间:2022-07-16 18:10 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android中ImageView实现选择本地图片并显示功能
[b]运行结果:[/b] [img]http://files.jb51.net/file_images/article/201704/201704050956409.gif[/img] 模拟器图库就三张 没办法~画质挺感人~ 一个隐式意图 [b]布局文件:[/b]
<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" 
  /> 
</RelativeLayout> 
[b]MainActivity.class[/b]
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; 
 @Override 
 protected 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); 
 } 
 @Override 
 public void onClick(View v) { 
  // TODO Auto-generated method stub 
  switch (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; 
  } 
  } 
 } 
 @Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
  // TODO Auto-generated method stub 
  if (requestCode == 0x1 && resultCode == RESULT_OK) { 
   if (data != null) { 
    image_choose.setImageURI(data.getData()); 
   } 
  } 
  super.onActivityResult(requestCode, resultCode, data); 
 } 
} 
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程素材网!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部