所以我在一个片段中,我想从图库中获取图像并设置为CircleImageView。我开始意图并正确选择了图像,但是在onActivityResult上未设置图像。保留默认的src映像。这是代码
fragment_layout
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/photo"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/ic_launcher_background"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
/>
片段类
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
view = lf.inflate(R.layout.fragment_profile,container,false);
mContext = getActivity();
mProfilePhoto = (CircleImageView) view.findViewById(R.id.profile_photo);
view.findViewById(R.id.profile_photo).setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.profile_photo:
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
try {
i.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(i, "Select Picture"), 0);
}catch (ActivityNotFoundException ex){
ex.printStackTrace();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0 && resultCode == Activity.RESULT_OK){
try {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("return-data");
mProfilePhoto.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
实现此片段的活动具有onActivityResult,而只是在super()处进行了调用
还可以在加载图像后自动调整其大小以适合圆形图像视图?如果没有,我该怎么办?预先感谢
答案 0 :(得分:1)
将Glide
与ImageView
一起使用,而不是CircleImageView
,并将Bitmap
设置为如下所示的圆圈
第1步。添加依赖项
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
第2步。从数据中获取位图并置于图像视图中
Bitmap photo = (Bitmap) data.getExtras().get("return-data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
Glide.with(this)
.load(stream.toByteArray())
.apply(RequestOptions.circleCropTransform())
.into(imageView);
全部
答案 1 :(得分:1)
似乎我遇到了这个问题。我正在将您的库用于我的所有项目。
if (requestCode != RESULT_CANCELED){
if (requestCode == GALLERY_REQUEST){
Uri path = data.getData();
try {
photo = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
} catch (IOException e) {
e.printStackTrace();
}
mProfilePhoto.setImageBitmap(photo);}}
答案 2 :(得分:1)
为什么要尝试将数据打包获取?尝试将其获取为uri并将其设置为circularimageview。
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.profile_photo:
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(
Intent.createChooser(i, "Select Picture"), 0);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0 && resultCode == Activity.RESULT_OK&&data!=null){
Uri uri=data.getData();
mProfilePhoto.setImageURI(uri);
}
}