将图像从 activity 传递到另一个图像

Passing an image from an activity to another one

我想在单击按钮时将图像从一个 activity 传递到另一个。我在第一个 activity 中有一个按钮 "Show Image"。当我点击它时,它应该从我的项目的 mipmap 文件夹传递两个图像并转到第二个 activity 并在那个 activity 的 ImageView 上显示传递的图像之一。在第二个 activity 上,我有两个按钮,它们应该接收图像并在单击每个按钮时显示这些图像。我尝试使用 intent 来传递图像,但是它没有用。有没有其他方法可以将 mipmap 文件夹中的图像从一个 activity 发送到另一个?

这是我的代码: MainActivity.java

package com.example.abina.myapplication;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {

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

        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showmyImage();
            }
        });

    }
    public void showmyImage(){
        Intent intent = new Intent(this, Main2Activity.class);
        Bitmap bitmap; // your bitmap
        bitmap = null;
        ByteArrayOutputStream _bs = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
        intent.putExtra("byteArray", _bs.toByteArray());
        startActivity(intent);
    }
}

activity_main.xml

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="137dp"
        android:text="Show Image" />

</RelativeLayout>

Main2Activity.java

package com.example.abina.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

public class Main2Activity extends AppCompatActivity {

    Button image1;
    Button image2;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        image1 =(Button) findViewById(R.id.image1);
        image2 = (Button) findViewById(R.id.image2);
        imageView =(ImageView) findViewById(R.id.imageView);


        if(getIntent().hasExtra("byteArray")) {
            Bitmap _bitmap = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
            imageView.setImageBitmap(_bitmap);
        }
    }
}

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

    <Button
        android:id="@+id/image1"
        android:layout_width="199dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Image1" />

    <Button
        android:id="@+id/image2"
        android:layout_width="183dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="0dp"
        android:text="Image2" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

图片

首先,mipmap 文件夹仅用于放置 app/launcher 图标(显示在主屏幕上)。您使用的任何其他可绘制资源都应放在相关的可绘制文件夹中。

下一步,因为它们将在您的可绘制对象中,所以我将传递 @DrawableRes id 例如R.id.image_name 值。

Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
startActivity(intent);

此外,我建议您使用 public static 变量 IMAGE_RES_ID_KEY 作为您的额外密钥以避免拼写错误。

然后在另一边你可以简单地

if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
    imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
}