在Android中使用CircleImageView访问图库

时间:2018-11-02 19:56:34

标签: java android mediastore

当我按图像时,什么也没有发生! enter image description here 这是我的代码:

public class Profile_Iamge extendes AppCompatActivity {

private CircleImageView profile;
private static final int PICK_IMAGE = 1;
Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.my_profile);

profile = (CircleImageView)findViewById(R.id.Profile_Img);
profile.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v){
    Intent gallery = new Intent();
    gallery.setType("image/*");
    gallery.setAction(Intent.ACTION_GET_CONTENT);

    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    pickIntent.setType("image/*");

    Intent chooserIntent = Intent.createChooser(gallery, "Select Image");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

    startActivityForResult(Intent.createChooser(chooserIntent, "select Picture"), PICK_IMAGE);
  }
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == PICK_IMAGE && resultCode == RESULT_OK){
  imageUri = data.getData();
  try{
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
    profile.setImageBitmap(bitmap);
  }catch (IOException e){
    e.printStackTrace();
  }
}
}
}

我在清单中添加了以下代码。

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

,并且在我的build.gradle中添加了实现“ de.hdodenhof:circleimageview:2.2.0”。我不知道怎么了。请有人帮我吗?

0 个答案:

没有答案