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

时间:2018-10-21 19:46:31

标签: android

我是android的新手。我想将一个活动中的图片传递给另一个活动。我尝试了很多次。但是我的应用程序崩溃了。

发件人活动:

camera=(ImageButton)findViewById(R.id.camera);
            gallery=(ImageButton)findViewById(R.id.gallery);
      camera.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View view) {
              Intent i =new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
              startActivityForResult(i, 50);
          }
      });

    gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(pickPhoto , 40);
        }
    });
}

接收者活动:

package com.androidlink.navigation_bottom;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;


import android.os.Bundle;
import android.widget.ImageView;
public class Image_set_Activity extends AppCompatActivity 
{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_set_);
    ImageView IV = (ImageView) findViewById(R.id.simpleImageView);

  }
}

2 个答案:

答案 0 :(得分:0)

尝试覆盖onActivityResult()。进一步阅读-https://developer.android.com/training/basics/intents/result

答案 1 :(得分:-1)

  

尝试使用此代码发送图像

ImageView imageview = (ImageView) findViewById(R.id.Tab);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
final byte[] arr = bitmap.getNinePatchChunk();

imageView.setOnClickListener(View v)
{
Intent intent = new Intent (MainActivity.this,bbb.class);
Intent.putExtra(“image”,arr);
startActivity(intent);
}
  

用于获取图像

Intent I = getIntent();
byte[] arr1 = i.getByteArrayExtra(“image”);
Bitmap map = BitmapFactory.decodeByteArray(arr,0,arr.length);
imageView.setImageBitmap(map);
相关问题