将图像从一个活动发送到另一个活动图像视图

时间:2015-01-05 09:34:35

标签: android

在第一个活动中,相机在surfaceview中打开,单击图像时单击捕获按钮,其预览显示在imageview的下一个活动上.Plz帮助我

我的第一项活动:

public class CodeActivity extends Activity实现了SurfaceHolder.Callback {

Button btn;
ImageView imgss;
public static final int SELECT_PICTURE = 1;
String selectedImagePath;
Context context;
Bitmap image;
Bundle bundle;
Intent intent;
String pictureFile;
String fileName;
private static int RESULT_LOAD_IMAGE = 1;

SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
//public static final int SELECT_PICTURE = 1;
private Camera mCamera;
//private CameraPreview mPreview;
private PictureCallback mPicture;
private Button capture, switchCamera;
private Context myContext;
//private LinearLayout cameraPreview;
private boolean cameraFront = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_code);
    myContext = this;
    context = this;

     //getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


    btn = (Button)findViewById(R.id.button1);
    imgss = (ImageView) findViewById(R.id.imageView1);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        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);*/

            mCamera.takePicture(null, null,  myPictureCallback_JPG);


        }
    });

    imgss.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        //sendImage();  
        }
    });




}


/*PictureCallback myPictureCallback_JPG = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data,0,data.length);
        File imageDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "surbeyImg");
        imageDirectory.mkdirs();
        File imgName = null;
        File f = new File(Environment.getExternalStorageDirectory()
         + File.separator + "surbeyImg" + File.separator + imgName+".jpg");
         try 
         {
                f.createNewFile();
                //write the bytes in file
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(data);
          } catch (IOException e) 
          {
            e.printStackTrace();
          }
        Intent intentImg = new Intent(CodeActivity.this, PassActivity.class);
         intentImg.putExtra("img",imgName);
         startActivity(intentImg); 

    }
};*/


/*PictureCallback myPictureCallback_JPG = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        String fileName = "tempIMG.png";
        try {
            FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
            fileOutStream.write(data);
            fileOutStream.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        Intent i = new Intent(context, PassActivity.class);

        Bundle bundle = new Bundle();
        bundle.putByteArray("photo", fileName);
        i.putExtras(bundle);
        startActivity(i);
    }

}; * /

PictureCallback  myPictureCallback_JPG = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
    Intent i = new Intent(context, PassActivity.class);

    Bundle bundle = new Bundle();
    bundle.putByteArray("photo", data);
    i.putExtras(bundle);
    startActivity(i);
}

};

我的下一个活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pass);


    /*imgss = (ImageView) findViewById(R.id.imagereceieve);
    btn = (Button)findViewById(R.id.button1);

    Bundle extras = getIntent().getExtras();
    String photoPath = extras.getString("photoPath");
    File filePath = getFileStreamPath(photoPath);

    imgss.setImageBitmap(BitmapFactory.decodeFile(photoPath));*/


    Bundle extras = getIntent().getExtras();
    byte[] photo = extras.getByteArray("photo");


    Bitmap bitmap  = decodeByteArray (photo, 0, photo.length);
    ImageView imgView = (ImageView)findViewById(R.id.imagereceieve);
    imgView.setImageBitmap(bitmap);

1 个答案:

答案 0 :(得分:2)

我认为你应该首先初始化字节数组。所以在分配之前,改变你的代码

Bundle extras = getIntent().getExtras();
byte[] photo = new byte [2048];
photo = extras.getByteArray("photo");
Bitmap bmp = BitmapFactory.decodeByteArray(photo , 0, photo .length);
imageview.setImageBitmap(bmp);        

在内部按钮中单击添加此代码

Intent i = new Intent(current_activity.this, Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putByteArray("photo", photo); // photo is an byte array you already stored data in it
i.putExtras(bundle);
startActivity(i);

接收下一个活动的图片

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_activty);
Bundle extras = getIntent().getExtras();
byte[] photo = new byte [2048];
photo = extras.getByteArray("photo");
Bitmap bmp = BitmapFactory.decodeByteArray(photo , 0, photo .length);
imageview.setImageBitmap(bmp);   
}