从图库中选择图像

从图库中选择图像

我正在尝试从图库中选择图像并带来图片,我可以带来4.4.2版本,但5.0.0或更高版本不起作用。

单击imageview1时:

imageview1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent galeri_int = new Intent();
                galeri_int.setType("image/*");
                galeri_int.setAction(Intent.ACTION_GET_CONTENT);
                galeri_int.addCategory(Intent.CATEGORY_OPENABLE);


                startActivityForResult(galeri_int,44);

                Log.d("tık","tıklandı");


            }
        });


OnActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){


        InputStream stream = null;

        if(data  !=null){

        try {
            stream = getContentResolver().openInputStream(data.getData());
            bitmapx = BitmapFactory.decodeStream(stream);
            stream.close();
            Bitmap resized = resize(bitmapx,1000,1000);

           imageview.setImageBitmap(resized);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        }


    }


它不适用于5.0.0或更高版本,该怎么办?

最佳答案

尝试这个

Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Choose Picture"), 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode==RESULT_CANCELED)
    {
        // action cancelled
    }
    if(resultCode==RESULT_OK)
    {
        Uri selectedimg = data.getData();
        imageView.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg));
    }
}

10-08 06:14