【问题标题】:Passing an image from an activity to another one将图像从活动传递到另一个活动
【发布时间】:2018-08-24 16:17:21
【问题描述】:

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

这是我的代码: 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 资源中,您可以通过附加功能将图像的资源 id 传递给下一个活动。无需将它们转换为字节数组。

标签: android android-intent android-imageview android-image


【解决方案1】:

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

接下来,因为它们将在您的可绘制对象中,所以我将传递 @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));
}

【讨论】:

  • 如何声明IMAGE_RES_ID_KEY?
  • 在MainActivity顶部,声明一个变量public static String IMAGE_RES_ID_KEY = "IMAGE_RES_ID_KEY";
  • 您可以将它们作为IMAGE_RES_ID_KEY1 和IMAGE_RES_ID_KEY2 传递,或者您可以传递一个int 数组IMAGE_RES_IDS_KEY 然后执行int[] ids = new int[] {R.id.image1, R.id.image2}; and then pass the int array intent.putExtra(IMAGE_RES_IDS_KEY, ids);`
猜你喜欢
  • 2012-03-05
  • 1970-01-01
  • 2012-07-16
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多