我构建了这个应用程序,我希望我可以从图库中选择一个图像来替换顶部的图像视图。就像 facebook 上的个人资料等...
我试过上面的代码:
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
Uri myPicture = null;
Button buttonLoadImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting View
buttonLoadImage =(Button) findViewById(R.id.regButton);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.regUserPhoto);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
在我的 android 4.4.4 手机中:我可以选择图像,但是当我按下 ok 时,应用程序崩溃了。
在模拟器 android 5.1.1 中:当我按下图像时,应用程序崩溃
慕容708150
相关分类