代码如下:
main.java:
//调用系统相册
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE);
}
//这里获取图片路径并显示
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex);
ImagePath=columnIndex;
showImage(imagePath);//调用图片显示方法
c.close();
}
}
//显示图片
private void showImage(String imaePath){
Bitmap bm = BitmapFactory.decodeFile(imaePath);
((ImageView)findViewById(R.id.ImageView01)).setImageBitmap(bm);
}
main.xml:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/open"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
到底是哪里出问题了呢?