Get/pick an image from Android's built-in Gallery app programmatically

前端 未结 19 1157
终归单人心
终归单人心 2020-11-22 00:49

I am trying to open an image / picture in the Gallery built-in app from inside my application.

I have a URI of the picture (the picture is located on the SD card).

19条回答
  •  渐次进展
    2020-11-22 01:14

    Please find the answer for the selecting single image from gallery

    import android.app.Activity;
    import android.net.Uri;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    
    public class PickImage extends Activity {
    
        Button btnOpen, btnGet, btnPick;
        TextView textInfo1, textInfo2;
        ImageView imageView;
    
        private static final int RQS_OPEN_IMAGE = 1;
        private static final int RQS_GET_IMAGE = 2;
        private static final int RQS_PICK_IMAGE = 3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.image_pick);
            btnOpen = (Button)findViewById(R.id.open);
            btnGet = (Button)findViewById(R.id.get);
            btnPick = (Button)findViewById(R.id.pick);
            textInfo1 = (TextView)findViewById(R.id.info1);
            textInfo2 = (TextView)findViewById(R.id.info2);
            imageView = (ImageView) findViewById(R.id.image);
    
            btnOpen.setOnClickListener(btnOpenOnClickListener);
            btnGet.setOnClickListener(btnGetOnClickListener);
            btnPick.setOnClickListener(btnPickOnClickListener);
        }
    
        View.OnClickListener btnOpenOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
    
                startActivityForResult(intent, RQS_OPEN_IMAGE);
            }
        };
    
        View.OnClickListener btnGetOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
    
                startActivityForResult(intent, RQS_OPEN_IMAGE);
            }
        };
    
        View.OnClickListener btnPickOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, RQS_PICK_IMAGE);
            }
        };
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == Activity.RESULT_OK) {
    
    
                if (requestCode == RQS_OPEN_IMAGE ||
                        requestCode == RQS_GET_IMAGE ||
                        requestCode == RQS_PICK_IMAGE) {
    
                    imageView.setImageBitmap(null);
                    textInfo1.setText("");
                    textInfo2.setText("");
    
                    Uri mediaUri = data.getData();
                    textInfo1.setText(mediaUri.toString());
                    String mediaPath = mediaUri.getPath();
                    textInfo2.setText(mediaPath);
    
                    //display the image
                    try {
                        InputStream inputStream = getBaseContext().getContentResolver().openInputStream(mediaUri);
                        Bitmap bm = BitmapFactory.decodeStream(inputStream);
    
                       ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        byte[] byteArray = stream.toByteArray();
    
                        imageView.setImageBitmap(bm);
    
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

提交回复
热议问题