0

Source Code for get image from gallery and set it to image view and i want to pass the selected image in another activity and set it to linear layout.

 private void galleryIntent() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);//
        startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
    }

    private void onSelectFromGalleryResult(Intent data) {
        bitmap = null;
        if (data != null) {
            filePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
                System.out.println("bitmap is :" +bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        set.setImageBitmap(bitmap);

    }
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
rs11
  • 65
  • 9
  • https://stackoverflow.com/questions/5309190/android-pick-images-from-gallery – MJM Jan 11 '18 at 12:52
  • i want to set selected gallary image to linear layout of another activity....please suggest me the code.. – rs11 Jan 11 '18 at 12:55

2 Answers2

0

Used from https://stackoverflow.com/a/26403116/8603832

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

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 2 && 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();

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

            if (bitmap != null) {
                ImageView rotate = (ImageView) findViewById(R.id.rotate);

            }

    }

pass selectedImage URI in Other Activity and using intent and use same code for generate the Bitmap with that URI

MJM
  • 5,119
  • 5
  • 27
  • 53
0
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();


         Intent i=new Intent(this,NextActivity.class);
    i.putExtra("path",picturePath);
    startActivity(i);



    }


}

NextActivity File :-

 String picturePath =getIntent().getStringExtra("path");

        LinearLayout imageView = (LinearLayout) findViewById(R.id.imgView);
        Bitmap bmImg = BitmapFactory.decodeFile(picturePath);
        BitmapDrawable background = new BitmapDrawable(bmImg);
        imageView.setBackgroundDrawable(background);
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
  • i want to pass that selected image on another activity and then set it to that linear layout background. @Prateek Bhardwaj – rs11 Jan 11 '18 at 13:17