【问题标题】:how to send image from one activity to other?如何将图像从一项活动发送到另一项活动?
【发布时间】:2013-03-24 19:32:06
【问题描述】:

我需要像发送字符串“title”一样发送一个imageview,但我不能,我怎样才能将一个imageview(R.drawable.image)从mainactivity发送到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); 
}

【问题讨论】:

  • 您是要发送ImageView,还是只是图片本身?

标签: android android-activity imageview send


【解决方案1】:

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

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

如果您在使用 ImageView 的文件路径时遇到问题。 Show Image View from file path?


解决方案 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); 
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      保存文件路径

      intent.putExtra("imagePath", filepath); 
      

      通过意图发送图像并使用

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

      【讨论】:

        【解决方案4】:

        把图片的路径放到putExtra中。不要发送位图可能很重

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-11
          • 2016-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多