如何使用意图将图像(jpg)从一个活动传递到另一个活动?

时间:2017-10-16 09:53:51

标签: java android android-intent

大家好我的问题是我无法将我的图片(Jpg)从一个活动转移到其他活动。我被困在我的项目中间所以请帮助我。

Activity1.java

- (IBAction)rotatePic:(id)sender 
{

_imageView.transform = CGAffineTransformMakeRotation(M_PI);

}

Activity2.java

public void Story1(View view) {

        String link=getResources().getResourceName(R.drawable.image1of1);
        Intent in = new Intent(Activity1.this,Activity2.class);
        in.putExtra("image",link);
        startActivity(in);
}

2 个答案:

答案 0 :(得分:1)

尝试以整数形式发送id

Activity1.java

public void Story1(View view) {

    int link=R.drawable.image1of1;
    Intent in = new Intent(Activity1.this,Activity2.class);
    in.putExtra("image",link);
    startActivity(in);
}

Activity2.java

protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story_cat1);

        i1 = (ImageView) findViewById(R.id.img1);

        Intent in = getIntent();
        Bundle b = in.getExtras();

        int img=getIntent().getIntExtra("image",-1);
        i1.setImageResource(img);
}

答案 1 :(得分:0)

您需要以字节数组的形式发送, 发送,

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_name);     
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] data = os.toByteArray();

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("image", data);
startActivity(intent);

在Activity2中接收,

Bundle extras = getIntent().getExtras();
byte[] data = extras.getByteArray("image");

然后在第二个活动中转换为位图,

Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

最后设置为imageview,

imageView.setImageBitmap(bmp);

** *尝试使用ID发送和接收

发送, 使用 - getResources().getIdentifier(image name,"drawable", .getPackageName());获取ID 发送 - intent.putExtra("image",id);

接收 - intent.getIntExtra("image",-1); 得到绘画 - getResources().getDrawable(id_in_activity2);

然后设置为imageview