[英]How can I access to gallery and pick image without startActivityForResult/onActivityResultin?
我有一个带有溢出菜单的CardView(玩家的CardView)。 该菜单具有编辑和删除功能,当您单击编辑时,会启动一个对话框,并且此对话框中有很多此播放器的属性。 其中之一是ImageView(玩家的照片),当您单击启动Intent时,您可以在图库中选择照片。 我的问题是如何获取该图像并设置ImageView?
在另一个线程中,我看到它们使用了startActivity
和onActivityResult
,但是在CardView中,我无法使用此方法。
我无法使用onActivityResult和startActivity来管理从画廊中挑选的图片
谢谢!
如果您不能使用onActivityResult,那么可以使用一种解决方案。
再次创建一个Activity(PickImageActivity)
将ResultReceiver作为参数存储在Intent中
从该活动的onCreate启动startActivityForResult
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
在onActivityResult中,您将具有以下内容:
Bitmap received = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
Bundle result = new Bundle();
result.putParcelable(RESULT_KEY, received);
((ResultReceiver) getIntent().getParcelableExtra(RECEIVER_KEY)).send(RESULT_OK, result);
finish();
因此,您可以像这样开始:
Intent pickImage = new Intent(this, PickImageActivity.class);
pickImage.putExtra(RECEIVER_KEY, new ResultReceiver() {
public void onReceive(int resultCode, Bundle data) {
//Work with the data received here trough RESULT_KEY
}
});
如果位图不适合您的内存,请发送内容文件路径。
此解决方案确实有效,并且我已经在项目中实现了该解决方案,但是将其重构为activityResult原因,如果先前的Activity被破坏,您将不会收到数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.