简体   繁体   中英

How to send image one activity to another activity?

I am new in android.i want to pass image in one activity to another activity.I tried many time.but my app gonna crash.plz edit my code with necessary changes.

Sender activity:

camera=(ImageButton)findViewById(R.id.camera);
            gallery=(ImageButton)findViewById(R.id.gallery);
      camera.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View view) {
              Intent i =new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
              startActivityForResult(i, 50);
          }
      });

    gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(pickPhoto , 40);
        }
    });
}

Receiver activity:

package com.androidlink.navigation_bottom;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;


import android.os.Bundle;
import android.widget.ImageView;
public class Image_set_Activity extends AppCompatActivity 
{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_set_);
    ImageView IV = (ImageView) findViewById(R.id.simpleImageView);

  }
}

Try to override onActivityResult(). For further reading - https://developer.android.com/training/basics/intents/result

Try this code For sending the image

ImageView imageview = (ImageView) findViewById(R.id.Tab);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
final byte[] arr = bitmap.getNinePatchChunk();

imageView.setOnClickListener(View v)
{
Intent intent = new Intent (MainActivity.this,bbb.class);
Intent.putExtra(“image”,arr);
startActivity(intent);
}

For getting the image

Intent I = getIntent();
byte[] arr1 = i.getByteArrayExtra(“image”);
Bitmap map = BitmapFactory.decodeByteArray(arr,0,arr.length);
imageView.setImageBitmap(map);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM