[英]How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio
I am trying to modify an image of an ImageView in one activity using a spinner, and then send that image to an ImageButton in another activity. 我正在尝试使用微调器在一个活动中修改ImageView的图像,然后将该图像发送到另一个活动中的ImageButton。
Here is the code of the ImageView activity: 这是ImageView活动的代码:
imgView = (ImageView) findViewById(R.id.imgView);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent imgIntent = getIntent();
switch (position) {
case 0:
imgView.setImageResource(R.drawable.brown);
imgIntent.putExtra("image", R.drawable.brown);
break;
case 1:
imgView.setImageResource(R.drawable.blue);
imgIntent.putExtra("image", R.drawable.blue);
break;
case 2:
imgView.setImageResource(R.drawable.black);
imgIntent.putExtra("image", R.drawable.black);
break;
case 3:
imgView.setImageResource(R.drawable.white);
imgIntent.putExtra("image", R.drawable.white);
break;
default:
imgView.setImageResource(R.drawable.brown);
imgIntent.putExtra("image", R.drawable.brown);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Here is the code of the ImageButton activity: 这是ImageButton活动的代码:
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
Intent imgIntent = getIntent();
imgBtn.setImageResource(imgIntent.getIntExtra("image"));
I cannot figure out how to do this. 我不知道该怎么做。
Full code of MainActivity: MainActivity的完整代码:
public class MainActivity extends AppCompatActivity { 公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
Intent imgIntent = getIntent();
imgBtn.setImageResource(imgIntent.getIntExtra("image", 0));
startActivity(imgIntent);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CustomizeDoggo.class);
startActivity(intent);
}
});
}
} }
just change this line.You haven't passed the default value 只需更改此行即可。您尚未传递默认值
imgBtn.setImageResource(imgIntent.getIntExtra("image",0));
and u haven't started the activity 而且你还没有开始活动
startActivity(mIntent);
UPDATE UPDATE
Start an intent like this 开始这样的意图
//This line is missing in your code
Intent intent = new Intent(CurrentActivity.this,ActivityToOpen.class);
intent.putExtra("image",id of selected image);
startActivity(intent);
在dest Activity中:在onCreate()
方法中调用getArgurment()
以获取包,然后在getIntExtra("image",default value);
wait try this will surely work 等待尝试这肯定会工作
In main activity while starting new activity use this code instead of yours 在开始新活动的主要活动中,使用此代码代替您的代码
Intent=new Intent(mContext,SecondActivity.class);
intent.putExtra("image",R.drawable.blue);
startActivity(intent);
now in second activity in oncreate use this code 现在在oncreate的第二个活动中使用此代码
int image=getIntent().getExtras().getInt("image",0);
mPlayPauseImageButton.setImageResource(image);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.