我尝试通过路径将图像发送到另一个活动。没有错误,但图像不可见。我认为这可能是发送路径的问题,但我不知道。图像在目录中可绘制。
感谢您的帮助
这是我的第一个活动:
public void onClickObamaBtn(View v){
Intent intent = new Intent(this, EkranB.class);
String path = getString(R.drawable.obama);
Toast.makeText(this, path, Toast.LENGTH_LONG).show();
intent.putExtra("president", path);
startActivity(intent);
}
Ant这是第二个:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ekran_b);
ivObama = (ImageView) findViewById(R.id.iV1);
preferences = this.getSharedPreferences("prezydenciUsa", Context.MODE_PRIVATE);
String path = getIntent().getStringExtra("president");
Drawable president = Drawable.createFromPath(path);
ivObama.setImageDrawable(president);
}
答案 0 :(得分:0)
您所说的路径是Resource
中的Drawable
,它是Resource id
的{{1}}
您可以直接从int
这样设置图像
Resource
我不明白,为什么您需要传递 imageView.setImageDrawable(getResources().getDrawable(R.id.obama));
,因为所有Resource id
如果你真的需要通过意图传递drawable,那么就这样做
Activity
现在,获取资源ID并将其设置为 int pathId = getResources().getDrawable(R.id.obama);
intent.putExtra("president", pathId);
,如此
ImageView
答案 1 :(得分:0)
in this case its better to pass integer value which you will get from first activity as follow
public void onClickObamaBtn(View v){
Intent intent = new Intent(this, EkranB.class);
int path = R.drawable.obama;
Toast.makeText(this, ""+path, Toast.LENGTH_LONG).show();
intent.putExtra("president", path);
startActivity(intent);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ekran_b);
ivObama = (ImageView) findViewById(R.id.iV1);
preferences = this.getSharedPreferences("prezydenciUsa", Context.MODE_PRIVATE);
int path = getIntent().getIntExtra("president",0);
ivObama.setImageDrawable(getResources().getDrawable(path));
}