Java 拍照后如何在其他活动中显示图像?

Java 拍照后如何在其他活动中显示图像?,java,android,image,camera,display,Java,Android,Image,Camera,Display,我想要我在另一个活动中拍摄并展示的照片。我更希望有一个按钮用于下一个活动,当单击时,显示我拍摄的图像。只需要一个简单的方法。我是android studio的新手,因此我不太确定如何解决这个问题 这是我的密码 Scanner.java activity_scanner.xml 尝试将照片转换为字节,并将其发送到其他活动 ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.Comp

我想要我在另一个活动中拍摄并展示的照片。我更希望有一个按钮用于下一个活动,当单击时,显示我拍摄的图像。只需要一个简单的方法。我是android studio的新手,因此我不太确定如何解决这个问题

这是我的密码

Scanner.java activity_scanner.xml


尝试将照片转换为字节,并将其发送到其他活动

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();

Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("photo", image);
startActivity(intent);
并在下一个活动中获取此信息,使用decodeByteArray创建位图并设置到图像视图中

YourActivity.class

    byte[] byteArrayExtra = getIntent().getByteArrayExtra("photo");

    //BitmapOptions is optional you can create bitmap without this also. This is the description of its use from google developer docs.
    //BitmapFactory.Options: null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.

    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length, new BitmapFactory.Options());

    //or
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length);


    imageview.setimageBitmap(bitmap);

尝试将照片转换为字节并发送到其他活动

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();

Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("photo", image);
startActivity(intent);
并在下一个活动中获取此信息,使用decodeByteArray创建位图并设置到图像视图中

YourActivity.class

    byte[] byteArrayExtra = getIntent().getByteArrayExtra("photo");

    //BitmapOptions is optional you can create bitmap without this also. This is the description of its use from google developer docs.
    //BitmapFactory.Options: null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.

    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length, new BitmapFactory.Options());

    //or
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length);


    imageview.setimageBitmap(bitmap);

Sry,我指的是代码的第一部分。以ByteArrayOutputStream开头的。我是否将此代码放在onActivityResult方法中?Sry,我指的是代码的第一部分。以ByteArrayOutputStream开头的。我是否将此代码放在onActivityResult方法中?