Select image from gallery and show in imageview

后端 未结 3 737
一个人的身影
一个人的身影 2020-12-12 06:48

I need Open my gallery in mobile phone and select one picture which opened in imageview on activity.. nothing hard.. but I have code and thise code in simulator (genymotion)

相关标签:
3条回答
  • 2020-12-12 06:49

    Did you try this?

    public void onActivityResult( ....) {
          If ( resultCode == RESULT_OK) {
                 Uri selectedImg = data.getData();
                 Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver, selectedImg);
                 If(bitmap != null) {
                    yourImageView.setImageBitmap(bitmap);
                 }
           }
    }
    
    0 讨论(0)
  • 2020-12-12 07:04

    put this code inside (anything which you use to go to gallery)

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

    Then call this function

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();
    
               Bitmap img=MediaStore.Images.Media.getBitmap(getContentResolver, filePath);
    
    
         If(img!= null) {
                image.setImageBitmap(img);
        }
    }
    

    don't forget this permission to be called

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2020-12-12 07:06

    ImagePicker : please credit the developer when using their library

    In your if statement inside of onActivityResult change requestCode to requestCode & 0xffff.

    What many people don't notice here is that the requestCode that is coming back is coming back as a hex number not as a digit number.

    In your AndroidManifest.xml add these permissions:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    

    Use this method to pick up images:

    protected  void onImageViewClick(){
      //  ImageView imageView=(ImageView)findViewById(R.id.imageView2);
        TextView tw=(TextView)findViewById(R.id.addimage);
        tw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectImage();
            }
        });
    
    }
    
    
    private void selectImage() {
        Intent takeImageIntent = ImagePicker.getPickImageIntent(this);
        if (takeImageIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            startActivityForResult(takeImageIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    

    Then use this to receive them:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bitmap bitmap = ImagePicker.getBitmapFromResult(this, resultCode, data);
        if (null != bitmap && resultCode == RESULT_OK) {
        //do what you want with the bitmap here
    
       }
    }
    
    0 讨论(0)
提交回复
热议问题