收到previous活动的照片,以当前的 [英] Receive photo from previous activity to the current one

查看:121
本文介绍了收到previous活动的照片,以当前的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code启动相机拍摄照片并发送的意图另一个活动:

This is my code to launch the camera and take a photo and send as Intent to another activity:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view =  inflater.inflate(R.layout.fragment_add_image, container, false);
    button = (Button)view.findViewById(R.id.addImage);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(),"Launching Camera",Toast.LENGTH_SHORT).show();
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra("putSomething", true);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
    return view;
}

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

    super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == Activity.RESULT_OK)
            {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                //ImageView jpgView = (ImageView)findViewById(R.id.imageView1);


                Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options);

                Intent imagepass = new Intent(getActivity(),StoreImage.class);
                imagepass.putExtra("imagepass", receipt );
                startActivity(imagepass);
            }
    }

现在在接收活动如何显示由摄像机拍摄到的照片,我应该怎么添加到XML文件中的GridView显示图像

Now in the receiving activity how do i show the photo captured by the camera, what should i add to the xml file to display the image in a GridView

这是我的code:

public class StoreImage extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store_image);
    //creating view ids


    Bitmap receiptimage = (Bitmap) getIntent().getExtras().getParcelable("imagepass");
    receipt.setImageBitmap(receiptimage);
}

}

推荐答案

如果你把它当作一个Parcelable,你一定会得到一个JAVA粘结剂失败错误。所以,解决的办法是这样的:如果位图的小像,说,缩略图,将它作为一个字节数组,并建立显示位图中的下一个活动。例如:

If you pass it as a Parcelable, you're bound to get a JAVA BINDER FAILURE error. So, the solution is this: If the bitmap is small, like, say, a thumbnail, pass it as a byte array and build the bitmap for display in the next activity. For instance:

在您的电话......活动

in your calling activity...

Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);

...并在接收活动

...and in your receiving activity

if(getIntent().hasExtra("byteArray")) {
    ImageView previewThumbnail = new ImageView(this);
    Bitmap b = BitmapFactory.decodeByteArray(
        getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);        
    previewThumbnail.setImageBitmap(b);
}

这篇关于收到previous活动的照片,以当前的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆