从一个活动图像发送到Android的另一个使用列表视图项 [英] send image from one activity to another using listview item in android

查看:144
本文介绍了从一个活动图像发送到Android的另一个使用列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我取出由URL数据到列表视图,我想,只要用户点击列表视图项目,然后选择项目的详细信息将显示给用户提供图像一起和我对文本数据发送到另一个活动,但无法发送图像,但在singleitem活动我收到空白,而不是imageview的形象,我怎么能解决这个problem.Some code,以供参考: -

  ListViewActivity code: -     公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
 //从选定的ListItem得到的值
字符串title =((的TextView)view.findViewById(R.id.title))的getText()的toString()。
串艺术家=((的TextView)view.findViewById(R.id.artist))的getText()的toString()。
字符串长度=((的TextView)view.findViewById(R.id.duration))。gettext的()。
    的toString();
串图象=((ImageView的)view.findViewById(R.id.list_image))
    .getImageMatrix()的toString()。//开始新意图
在意向=新意图(getApplicationContext(),SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE,职称);
in.putExtra(KEY_ARTIST,艺术家);
in.putExtra(KEY_DURATION,持续时间);
in.putExtra(KEY_THUMB_URL,图像);
startActivity(在);     SingleItemActivity code: -
    //获取数据的意图
    在意向= getIntent();    //从previous意图XML值    字符串title = in.getStringExtra(KEY_TITLE);
    串艺术家= in.getStringExtra(KEY_ARTIST);
    字符串长度= in.getStringExtra(KEY_DURATION);
    位图位=(位图)in.getParcelableExtra(KEY_THUMB_URL);
    //在屏幕上显示的所有值
    TextView的lblName =(的TextView)findViewById(R.id.name_label);
    TextView的lblCost =(的TextView)findViewById(R.id.email_label);
    TextView的lblDesc =(的TextView)findViewById(R.id.mobile_label);
    ImageView的图像=(ImageView的)findViewById(R.id.image_label);
    image.setImageBitmap(位图);
    lblName.setText(职称);
    lblCost.setText(艺术家);
    lblDesc.setText(持续时间);     SingleItemXML code: -
     <的LinearLayout
      的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      机器人:方向=垂直
      机器人:layout_width =match_parent
      机器人:layout_height =match_parent>    < ImageView的
        机器人:ID =@ + ID / image_label
        机器人:layout_width =50dp
        机器人:layout_height =50dp
        机器人:SRC =@绘制/蕾哈娜
        />
        < TextView的机器人:ID =@ + ID / name_label
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =25dip
        机器人:文字样式=大胆
        机器人:paddingTop =10dip
        机器人:paddingBottom会=10dip
        机器人:文字颜色=#43bd00/>        < TextView的机器人:ID =@ + ID / EMAIL_LABEL
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#acacac/>       < TextView的机器人:ID =@ + ID / mobile_label
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字样式=大胆/>        < / LinearLayout中>


解决方案

in.putExtra(KEY_THUMB_URL,图像);

将图像转换为BASE64字符串格式。在新的活动重新转换BASE64字符串中的图像。

 公共字符串bitMapToString(位图位图){
        ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
        bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
        字节[] B = baos.toByteArray();
        字符串stringBody = NULL;
        尝试{
            stringBody = Base64.en codeToString(B,Base64.DEFAULT)的ToString();
        }赶上(例外五){
            吐司(e.toString());
            e.printStackTrace();
        }
        返回stringBody;
    }

I am fetching data from URL into listview and i want whenever user will click on listview item then selected item details will be shown to user along with image and i am to send text data to another activity but not able to send image, but in singleitem activity i am getting blank space instead of image in imageview, how can i resolve this problem.Some code for reference:-

     ListViewActivity Code:-   

    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();
String image = ((ImageView)view.findViewById(R.id.list_image))
    .getImageMatrix().toString();

// 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, image);
startActivity(in);

     SingleItemActivity Code:-
    // 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);
    Bitmap bitmap =(Bitmap) in.getParcelableExtra(KEY_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);     
    ImageView image = (ImageView)findViewById(R.id.image_label);
    image.setImageBitmap(bitmap);   


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

     SingleItemXML Code:-
     <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <ImageView     
        android:id="@+id/image_label"   
        android:layout_width="50dp"
        android:layout_height="50dp"
        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>

解决方案

in.putExtra(KEY_THUMB_URL, image);

Convert the image into BASE64 String format. and in new activity convert the BASE64 String in to image again..

public String bitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String stringBody = null;
        try {
            stringBody = Base64.encodeToString(b, Base64.DEFAULT).toString();
        } catch (Exception e) {
            toast(e.toString());
            e.printStackTrace();
        }
        return stringBody;
    }

这篇关于从一个活动图像发送到Android的另一个使用列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆