Umit Kaya
Umit Kaya

Reputation: 5951

Import image and pass to another activity

I have almost checked and tried all questions in here. But i can not pass the image I pick from a gallery to another activity. This is the onClickListener():

logoview=(ImageView)findViewById(R.id.logoview);
    logoview.setOnClickListener(new OnClickListener() 
    {            

    @Override
    public void onClick(View arg0) {

    openGallerylogo();
        }}); 

This is openGallery intent:

private void openGallerylogo() {
    // TODO Auto-generated method stub
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}

This is my onActivityResult():

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

    if (requestCode == 1) 
    {
        if (data != null && resultcode == RESULT_OK) 
        {              

            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 filePath = cursor.getString(columnIndex);
            cursor.close();
            if(bmp1 != null && !bmp1.isRecycled())
            {
                bmp1 = null;                
            }

            ImageView logoview = (ImageView) findViewById(R.id.logoview);
            logoview.setImageBitmap(BitmapFactory.decodeFile(filePath));
            bmp1 = BitmapFactory.decodeFile(filePath);

            if (count.equals("logo")){
                logoview.setBackgroundResource(0);
                logoview.setImageBitmap(bmp1);              
            }
            else {

                Log.d("Status:", "Photopicker canceled");    
            }}} 

This code does; click on ImageView, go gallery, pick image and set it on same ImageView in MainActivity. After i clicked the button to move to the next activity, I want to see this image is set on another ImageView in SecondActivity. Thank you.

Upvotes: 0

Views: 907

Answers (2)

Chintan Khetiya
Chintan Khetiya

Reputation: 16142

Write below code when you want to change your activity.

Sender Activity Activity_one.java

Intent newdata = new Intent(Activity_one.this, Activity_Two.class);
newdata.putExtra("picture_path", filePath);
startActivity(newdata);

Receiver Activity Activity_two.java

String pre_img_path= getIntent().getStringExtra("picture_path");
ImageView logoview = (ImageView) findViewById(R.id.logoview);
logoview.setImageBitmap(BitmapFactory.decodeFile(pre_img_path));

Upvotes: 2

Piyush
Piyush

Reputation: 18933

After set your image to ImageView. Do this way:

Bitmap myBitmap = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytepicture = baos.toByteArray();

Intent newdata = new Intent(MainMenu.this, YourNewActivity.class);
newdata.putExtra("picture", bytepicture);
startActivity(newdata);

And retrieve in your desired Activity on onCreate() as:

byte[] byteArray = extras.getByteArray("picture");
Bitmap image_bmp = BitmapFactory.decodeByteArray(byteArray, 0,
                byteArray.length);
yourimageview.setImageBitmap(image_bmp );

Upvotes: 1

Related Questions