【问题标题】:how to send image from one activity to another, using listview item如何使用列表视图项将图像从一个活动发送到另一个活动
【发布时间】:2012-09-16 20:53:28
【问题描述】:

将图像从一个活动发送到另一个活动但能够将文本发送到另一个活动的问题,放置一些带有文件名的源代码请查看并更正错误,如果可能的话,请写下所需的代码,因为我面临这个问题最近两天,我在stackoverflow中提交了这个问题3次,但我没有得到很好的答案,请有人写下我需要做的正确事情并为我编写所需的代码,但这里我放了一些我的代码:

MainActivity 代码:

 list.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
             // getting values from selected ListItem
                String title = ((TextView) view.findViewById
          (R.id.title)).getText().toString();
                String artist = ((TextView) view.findViewById
          (R.id.artist)).getText().toString();
                String duration = ((TextView) view.findViewById
          (R.id.duration)).getText().toString();
                byte[] image = null;
                Bitmap thumb_url = BitmapFactory.decodeByteArray
          (image, 0, image.length);


            //  Bitmap bMap = BitmapFactory.decodeByteArray
          (array, 0, array.length);

                // Starting new intent
                Intent in = new Intent
          (getApplicationContext(),    SingleMenuItemActivity.class);
                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_ARTIST, artist);
                in.putExtra(KEY_DURATION, duration);
                in.putExtra(KEY_THUMB_URL, thumb_url);
                startActivity(in);

            }
        });     
    }   
    }

接收活动代码:

public class SingleMenuItemActivity  extends Activity {

// XML node keys
private static final String KEY_TITLE = "title";
private static final String KEY_ARTIST = "artist";
private static final String KEY_DURATION = "duration";
private static final String KEY_THUMB_URL = "thumb_url";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);

    // getting intent data
    Intent in = getIntent();

    // Get XML values from previous intent
    String title = in.getStringExtra(KEY_TITLE);
    String artist = in.getStringExtra(KEY_ARTIST);
    String duration = in.getStringExtra(KEY_DURATION);

    ImageView image = (ImageView)findViewById(R.id.thumb_url);
    Object thumb_url = null;
    View bitmap = null;
    bitmap.setBackgroundResource((Integer) thumb_url);   
    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblCost = (TextView) findViewById(R.id.email_label);
    TextView lblDesc = (TextView) findViewById(R.id.mobile_label);

   //what code to write for image here 

    lblName.setText(title);
    lblCost.setText(artist);
    lblDesc.setText(duration);

}
 }

XML 文件代码:

    <ImageView     
        android:id="@+id/thumb_url"   
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/rihanna"/>


   <TextView android:id="@+id/name_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dip"
        android:textStyle="bold"
        android:paddingTop="10dip"
        android:paddingBottom="10dip"
        android:textColor="#43bd00"/>

      <TextView android:id="@+id/email_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#acacac"/>

    <TextView android:id="@+id/mobile_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"/>
 </LinearLayout>

【问题讨论】:

  • 你可以从intent中获取图片。

标签: android android-intent android-emulator android-listview android-activity


【解决方案1】:

我可能弄错了,但我认为您尝试有意传递的对象的大小是有限制的(失败的活页夹事务错误?)。

最好的变体是将此图像存储在某处(内存或磁盘缓存)并仅将图像链接传递给您的活动,因此它将从缓存中检索图像。即使第二个活动重新启动,您也可以轻松地将链接保存在某处(onSaveInstanceState),而无需使用原始位图进行操作。

【讨论】:

    【解决方案2】:

    试试这个

    1.

    Use Intent in = new Intent (your activity.this,SingleMenuItemActivity.class);

    而不是

    Intent in = new Intent(getApplicationContext(),SingleMenuItemActivity.class);

    2.

    Intent in = getIntent();
    
        // Get XML values from previous intent
        String title = in.getStringExtra(KEY_TITLE);
        String artist = in.getStringExtra(KEY_ARTIST);
        String duration = in.getStringExtra(KEY_DURATION);
        Bitmap bitmap =(Bitmap) in.getParcelableExtra(KEY_THUMB_URL);
    
        ImageView image = (ImageView)findViewById(R.id.thumb_url);
    
    
        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name_label);
        TextView lblCost = (TextView) findViewById(R.id.email_label);
        TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
    
        image.setImageBitmap(bitmap); 
    
        lblName.setText(title);
        lblCost.setText(artist);
        lblDesc.setText(duration);
    
    }
    

    【讨论】:

    • Object thumb_url = null; 有什么用查看位图 = null; bitmap.setBackgroundResource((Integer) thumb_url);?
    • 谢谢你,太好了,你已经解决了我 95% 的问题,如此有用的代码但仍然遇到问题,如果我写这个代码:- android:src="@drawable/ rihanna”在 xml 文件中,然后每次单击 listview 项目时只获取该特定图像,但是如果我删除此行,那么我会在图像视图位置获得空白,所以请告诉我我需要在 ImageView 的 singleviewitem.xml 文件中写入什么获取列表视图项目图像...再次感谢
    • @AshaSoman 是的,但我收到此错误:“键 thumb_url 预期 Parcelable 但值为 java.lang.String。返回默认值 。”代码是一样的。
    【解决方案3】:

    Bitmap 实现 Parcelable,因此您可以随时在 Intent 中传递它:

    Intent in = new Intent
          (getApplicationContext(),    SingleMenuItemActivity.class);
                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_ARTIST, artist);
                in.putExtra(KEY_DURATION, duration);
                in.putExtra(KEY_THUMB_URL, thumb_url);
                startActivity(in);
    

    并在另一端检索它:

    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
    image.setImageBitmap(bitmap); 
    

    【讨论】:

    • 是的,但我收到此错误:“键 thumb_url 预期 Parcelable 但值为 java.lang.String。返回默认值 。”代码是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-11-04
    相关资源
    最近更新 更多