将图像作为位图发送(通过Intent或Sharedpreference)

时间:2018-11-05 06:08:44

标签: 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 :(得分:-1)

onActivityResult

Bitmap photo = (Bitmap) data.getExtras().get("data");
            Log.d("===uploadPhoto","camera : "+photo);
            *//* Passing BITMAP to the Second Activity *//*
            Intent intentCamera = new Intent(this, SecondAvtivity.class);
            IntentCamera.putExtra("BitmapImage", photo);
            startActivity(intentCamera);  

in seconActivity

Bitmap fromCamera = getIntent().getParcelableExtra("BitmapImage");
        imageView.setImageBitmap(fromCamera)

答案 1 :(得分:-1)

尝试使用此代码

第一堂课

 ImageView img=findViewById(R.id.imgId);
 BitmapDrawable bmd= (BitmapDrawable) img.getDrawable();
 Bitmap bt= bmd.getBitmap();
 final byte[] imgarry = bt.getNinePatchChunk();

  Intent intent = new Intent (FirstClass.this,SecondClass.class);
  Intent.putExtra(“imgs”,imgarry);
  startActivity(intent);

第二堂课

 Intent in = getIntent();
 byte[] ary= in.getByteArrayExtra(“imgs”);
 Bitmap bm= BitmapFactory.decodeByteArray(ary,0,ary.length);
 imageView.setImageBitmap(bm);