使用 extras 从一个 activity 传递到另一个图像

Image passing from one activity to another using extras

谁能告诉我如何使用附加功能将图像从一个 activity 传递到另一个 activity(想法是)。我将 activity1 中的一组图像显示为水平滚动视图。当我点击一张图片时,它应该以完全不同的布局显示在另一个 activity (activity2) 中。

如果解释也用 json 完成,我会很满意 我已经尝试了这个 (http://www.vogella.com/tutorials/AndroidIntent/article.html) 教程,但找不到答案。

MAIN ACTIVITY:

package com.example.user.horizontal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
ImageView Display;

    @Override
     protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           ImageView im1 = (ImageView) findViewById(R.id.image1);
        ImageView im2 = (ImageView) findViewById(R.id.image2);
        ImageView im3 = (ImageView) findViewById(R.id.image3);
        ImageView im4 = (ImageView) findViewById(R.id.image4);
        ImageView im5 = (ImageView) findViewById(R.id.image5);
        ImageView im6 = (ImageView) findViewById(R.id.image6);
        ImageView im7 = (ImageView) findViewById(R.id.image7);


            im1.setOnClickListener(this);
            im2.setOnClickListener(this);
            im3.setOnClickListener(this);
            im4.setOnClickListener(this);
            im5.setOnClickListener(this);
            im6.setOnClickListener(this);
            im7.setOnClickListener(this);


       }

         public void onClick(View v) {

         Intent clickimage = new Intent(this, OnClick.class);

          switch (v.getId()) {

        case R.id.image1:
            Display.setImageResource(R.drawable.images1);
            clickimage.putExtra("display1", "images1");
            startActivity(clickimage);
            break;

        case R.id.image2:
            Display.setImageResource(R.drawable.images2);
            clickimage.putExtra("display2", "pic2filename");
            startActivity(clickimage);
            break;

        case R.id.image3:
            Display.setImageResource(R.drawable.images8);
            clickimage.putExtra("display3", "pic3filename");
            startActivity(clickimage);
            break;
        case R.id.image4:
            Display.setImageResource(R.drawable.images4);
            clickimage.putExtra("display4", "pic4filename");
            startActivity(clickimage);
            break;
        case R.id.image5:
            Display.setImageResource(R.drawable.images5);
            clickimage.putExtra("display5", "pic5filename");
            startActivity(clickimage);
            break;
        case R.id.image6:
            Display.setImageResource(R.drawable.images6);
            clickimage.putExtra("display6", "pic6filename");
            startActivity(clickimage);
            break;
        case R.id.image7:
            Display.setImageResource(R.drawable.images8);
            clickimage.putExtra("display7", "pic7filename");
            startActivity(clickimage);
            break;


            }


        }

第二个ACTIVITY:

package com.example.user.horizontal;

import android.app.Activity;
import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
import android.os.Bundle;
 import android.widget.ImageView;


 public class OnClick extends Activity{


private Bitmap mImage1;
private Bitmap mImage2;
private Bitmap mImage3;
private Bitmap mImage4;
private Bitmap mImage5;
private Bitmap mImage6;
private Bitmap mImage7;
private Bitmap mImage8;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.onclick);

    Bundle bundle = getIntent().getExtras();
    String imageName = bundle.getString("display");


    // click listeners
    ImageView imageView = (ImageView)findViewById(R.id.image1);

    mImage1 = BitmapFactory.decodeResource(getResources(), R.drawable.images1);
    mImage2 = BitmapFactory.decodeResource(getResources(), R.drawable.images2);
    mImage3 = BitmapFactory.decodeResource(getResources(), R.drawable.images4);
    mImage4 = BitmapFactory.decodeResource(getResources(), R.drawable.images5);
    mImage5 = BitmapFactory.decodeResource(getResources(), R.drawable.images6);
    mImage6 = BitmapFactory.decodeResource(getResources(), R.drawable.images7);
    mImage7 = BitmapFactory.decodeResource(getResources(), R.drawable.images8);
    mImage8 = BitmapFactory.decodeResource(getResources(), R.drawable.images);



    if(imageName.matches("images1")) {
        String b = bundle.getString("b");
        imageView.setImageBitmap(mImage1);
    }
    else if (imageName.matches("pic2filename")) {
        imageView.setImageBitmap(mImage2);
    }
    else if (imageName.matches("pic3filename")) {
        imageView.setImageBitmap(mImage3);
    }
    else if (imageName.matches("pic4filename")) {
        imageView.setImageBitmap(mImage4);
    }
    else if (imageName.matches("pic2filename")) {
        imageView.setImageBitmap(mImage5);
    }
    else if (imageName.matches("pic5filename")) {
        imageView.setImageBitmap(mImage6);
    }
    else if (imageName.matches("pic6filename")) {
        imageView.setImageBitmap(mImage7);
    }
    else if (imageName.matches("pic7filename")) {
        imageView.setImageBitmap(mImage8);
    }
      }

  }

我是 Java 和 Android 的新手。

使用 intent 传递位图:

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

并在另一端取回:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

不要在活动之间传递图像。 您应该在 Intent 中传递图像的路径并在新的 Activity 中显示它。这是正确的做法。

尝试使用 android

中的 extras 将图像文件路径或图像 url 传递给下一个 activity