如何在不同的活动之间发送图片?

5
我需要像发送字符串“title”一样发送ImageView,但我无法完成。如何从MainActivity将ImageView(R.drawable.image)发送到SecondActivity?谢谢。 主活动
public void NewActivity(View view){ 
Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("title", getString(R.string.title));    
startActivity(intent); 
} 

第二个活动

@Override 
public void onCreate(Bundle bundle) 
{ 

super.onCreate(bundle); 
setContentView(R.layout.pantalla); 
Bundle extras = getIntent().getExtras(); 
if (extras == null) 
{ 
return; 
} 
String title = extras.getString("title"); 

TextView textview = (TextView)findViewById(R.id.titulo); 

textview.setText(title); 
}

1
你想发送的是 ImageView 还是仅仅是图片本身? - dumbfingers
4个回答

6

解决方案1:(针对非drawable资源)

您可以将路径文件名作为字符串发送。就像您的示例中的"title"一样。

如果您在使用文件路径到ImageView方面遇到问题。 如何从文件路径显示ImageView?


解决方案2:(适用于drawable简单且轻便的方式)

发送资源整数值,例如:

主要活动

Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("resourseInt", R.drawable.image);    
startActivity(intent); 

第二个活动

@Override 
public void onCreate(Bundle bundle) 
{ 

    super.onCreate(bundle); 
    setContentView(R.layout.pantalla); 
    Bundle extras = getIntent().getExtras(); 
    if (extras == null) 
    { 
        return; 
    } 
    int res = extras.getInt("resourseInt"); 

    ImageView view = (ImageView) findViewById(R.id.something); 

    view.setImageResourse(res); 
}

3

保存文件路径

intent.putExtra("imagePath", filepath); 

通过 Intent 发送图像并使用

String image_path = getIntent().getStringExtra("imagePath");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
myimageview.setImageDrawable(bitmap);

3
把图片路径放在putExtra里面。不要发送bitmap,因为它可能很重。

0

可绘制对象在应用程序的所有活动中都可用。
您可以直接访问它们,而无需将它们从一个活动发送到另一个活动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接