如何将图像发送到Android中的另一个活动

时间:2015-07-13 11:10:55

标签: android android-intent android-activity

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;


public class MainActivity extends ActionBarActivity {

    Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView i = (ImageView) findViewById(R.id.iv_img);
    i.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ByteArrayOutputStream bStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
            byte[] byteArray = bStream.toByteArray();

            Intent anotherIntent = new Intent(MainActivity.this, anotherActivity.class);
            anotherIntent.putExtra("image", R.drawable.img);
            startActivity(anotherIntent);

        }
    });
}

1 个答案:

答案 0 :(得分:0)

您可以使用intent将byteArray发送到其他活动。

Intent anotherIntent = new Intent(MainActivity.this, anotherActivity.class);

anotherIntent.putExtra("image", byteArray);

startActivity(anotherIntent);

现在您必须将此bytearray接收到另一个活动。

Bitmap bmp;

byte[] byteArray = getIntent().getByteArrayExtra("image");

bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

imageview.setImageBitmap(bmp);