0

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

如果解释也用 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;


            }


        }

第二个活动:

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 的新手。

4

3 回答 3

2

尝试使用 android 中的附加功能将图像文件路径或图像 url 传递给下一个活动

于 2015-07-30T06:56:35.550 回答
0

使用意图传递位图:

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

并在另一端检索它:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
于 2015-07-30T06:56:08.927 回答
0

不要在活动之间传递图像。您应该传递图像的路径Intent并将其显示在新的Activity. 这是一个正确的方法。

于 2015-07-30T06:56:28.357 回答