Android读取系统相册图片并获得绝对地址
public class MainActivity extends Activity { private Button btn; private ImageView img; Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.select); img=(ImageView)findViewById(R.id.pic); //设置监听 setlistener(); } private void setlistener() { // TODO Auto-generated method stub btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setType("image/*");//set intent type intent.setAction(Intent.ACTION_GET_CONTENT); //取得图片信息返回MainActivity startActivityForResult(intent,1); } }); } protected void onActivityResult(int requestCode,int resultCode,Intent data) { if(resultCode==RESULT_OK) { Uri uri = data.getData(); //通过URI获取图片绝对地址 String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri,proj,null,null,null); int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); //游标跳到首位,防止越界 cursor.moveToFirst(); String img_path = cursor.getString(actual_image_column_index); //通过地址获得位图信息 bitmap =BitmapFactory.decodeFile(img_path); //设置ImageView资源 img.setImageBitmap(bitmap); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }