从图库中获取图像并设置为 CircleImageView
Take image from gallery and set to a CircleImageView
所以我在片段中,我想从图库中获取图像并设置为 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"
/>
片段class
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();
}
}
}
实现此片段的 activity 具有 onActivityResult ,仅调用 super();
另外,加载图片后,是否会自动调整大小以适应圆形图片视图?如果不是我该怎么办?提前致谢
使用 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);
全部
这个问题我好像遇到过。我的所有项目都在使用您的图书馆。
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);}}
Why are you trying to get the data as bundle?Try to get as uri and set it to the 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);
}
}
所以我在片段中,我想从图库中获取图像并设置为 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"
/>
片段class
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();
}
}
}
实现此片段的 activity 具有 onActivityResult ,仅调用 super();
另外,加载图片后,是否会自动调整大小以适应圆形图片视图?如果不是我该怎么办?提前致谢
使用 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);
全部
这个问题我好像遇到过。我的所有项目都在使用您的图书馆。
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);}}
Why are you trying to get the data as bundle?Try to get as uri and set it to the 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);
}
}