无法选择图库图片到图像按钮?

时间:2017-08-26 07:25:01

标签: android

虽然我已经获得了所需的权限,但我获得了图库的意图,但无法在Image-Button上设置图像。

imagg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
            gallery.setType("image/=");
            startActivityForResult(gallery, res);
        }
    });

     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == res && resultCode == RESULT_OK) {
        Uri imageurl = data.getData();
        imagg.setImageURI(imageurl);
    }

1 个答案:

答案 0 :(得分:0)

imagg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);

gallery.setType("image/*");
gallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gallery,"Select Picture"), 1);
        }
    });

     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == res && resultCode == RESULT_OK) {
        Uri imageurl = 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();

        bitmap = BitmapFactory.decodeFile(picturePath);
        image.setImageBitmap(bitmap);
    }