Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

android - How to load an image in image view from gallery?

I have an activity, which has a button. When I click on the button it redirects me to the image gallery. I want to show the selected image in the next activity using an image view. But it is not displaying the image. The view is off screen when the image is set.

My code for selecting image and moving on next is given below. I am using no history true in my activities.

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

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = 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();
        if (!(picturePath.equals(""))) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, ImageInGellary.class);
            intent.putExtra("picturePath", picturePath);
            startActivity(intent);

        }
    }
}

public class ImageInGellary extends Activity {
    Button cancel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.load_image);
        cancel = (Button) findViewById(R.id.buttonCancelPicture);
        Intent in = getIntent();
        savedInstanceState = in.getExtras();
        String picturePath = savedInstanceState.getString("picturePath");
        ImageView imageView = (ImageView) findViewById(R.id.img_view);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                /*
                 * Intent i = new Intent(Intent.ACTION_PICK,
                 * android.provider.MediaStore
                 * .Images.Media.EXTERNAL_CONTENT_URI);
                 * 
                 * startActivityForResult(i, RESULT_LOAD_IMAGE);
                 */
                Intent intent = new Intent();
                intent.setClass(ImageInGellary.this, MainActivity.class);
                startActivity(intent);

            }
        });
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
    public class ImageGalleryDemoActivity extends Activity {
         
         
        private static int RESULT_LOAD_IMAGE = 1;
         
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
             
            Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
            buttonLoadImage.setOnClickListener(new View.OnClickListener() {
                 
                @Override
                public void onClick(View arg0) {
                     
                    Intent i = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                     
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
                }
            });
        }
         
         
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
             
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = 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();
                 
                ImageView imageView = (ImageView) findViewById(R.id.imgView);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
             
            }
         
         
        }
    }

To support android 11 you need to add this code in AndroidMainfest.xml

        <queries>
            <intent>
                <action android:name="android.intent.action.GET_CONTENT" />
                <data android:mimeType="image/*"/>
            </intent>
        </queries>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...