【问题标题】:Image not loaded in next activity图片未在下一个活动中加载
【发布时间】:2013-09-10 14:05:58
【问题描述】:

我正在使用 json 方法在 listview 中获取文本和图像。我在列表视图中同时获取文本和图像。但我只在下一个活动中收到文本。我不知道如何在下一个活动中获取图像。任何人都可以帮助我...?

我的代码:

MainActivity

listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            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 desc = ((TextView) view.findViewById(R.id.desc)).getText().toString();
                String image = ((ImageView) view.findViewById(R.id.image1)).getImageMatrix().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra("TAG_TITLE", title);
                in.putExtra("TAG_DESC", desc);
                in.putExtra("TAG_IMAGE", image);

                startActivity(in);
            }
        });

    } 

SingleMenuItemActivity

公共类 SingleMenuItemActivity 扩展 Activity {

Bitmap bmimage = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_list);

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

    // Get JSON values from previous intent
    String title = in.getStringExtra("TAG_TITLE");
    String desc = in.getStringExtra("TAG_DESC");
    String image = in.getStringExtra("TAG_IMAGE");

    try {
        URL url = new URL(image);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bmimage = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


     TextView lbltitle = (TextView) findViewById(R.id.title);
     TextView lbldesc = (TextView) findViewById(R.id.desc);
     ImageView lblimage = (ImageView) findViewById(R.id.image1);

    // Displaying all values on the screen

    lbltitle.setText(title);
    lbldesc.setText(desc);
    lblimage.setImageBitmap(bmimage); 


}

}

【问题讨论】:

标签: android json listview


【解决方案1】:

最好的解决方案是不要通过 parcelable 发送位图,因为图像可能太大 - 并且某些设备中的应用程序会崩溃。

我建议只将位图保存在 sdcard 中 - 通过保存位图的附加链接/url 发送,在下一个活动中只需从 sdcard 加载位图。

Saving bitmap

Load bitmap

【讨论】:

    【解决方案2】:

    传递图像位图而不是字符串

    ImageView imageView = (ImageView) view.findViewById(R.id.image1);
    Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    
    // ...
    
    // Starting new intent
    Bundle extras = new Bundle();
    extras.putParcelable("TAG_IMAGE", image);
    in.putExtras(extras);
    startActivity(in);
    

    在第二个活动中,从包中读出它

    Bundle extras = getIntent().getExtras();
    Bitmap bmp = (Bitmap) extras.getParcelable("TAG_IMAGE");
    // ...
    lblimage.setImageBitmap(bmp );
    

    【讨论】:

    • 嗨@Nachi,我只希望在第一个活动中列表视图中的“标题”,然后我们单击列表视图中的任何一个项目(标题)意味着它将在第二个活动中显示该项目的所有详细信息。我试过了,但它不起作用。
    • 不清楚您要做什么。最好为此创建一个新问题。
    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多