我想要意图imageview

时间:2013-12-16 09:18:56

标签: android android-layout android-intent android-webview android-imageview

我希望将imageview视图用于新活动。我的imageview使用xmlparser。但如果我点击imageview,我的应用程序就会关闭。

这是我的代码:

   thumb_image.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                 String title = ((TextView) findViewById(R.id.textURL)).getText().toString();
                 String date = ((TextView) findViewById(R.id.dateURL)).getText().toString();
                 String body = ((TextView) findViewById(R.id.bodyURL)).getText().toString();
                 ImageView thumb_image=(ImageView)findViewById(R.id.ImageURL); // thumb image
                 thumb_image.buildDrawingCache();
                 Bitmap image= thumb_image.getDrawingCache();
                  Bundle extras = new Bundle();
                 extras.putParcelable(KEY_IMAGEURL, image);
                 // Starting new intent
                 Intent i = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                 i.putExtra(KEY_HEADLINE, title);
                 i.putExtra(KEY_ARTICLEDATE, date);
                 i.putExtra(KEY_BODY,body);
                 i.putExtras(extras);

                 startActivity(i);

我的单一活动代码

   Intent in = getIntent();
Bundle bun = in.getExtras();

 //Get XML values from previous intent
String headline = bun.getString(KEY_HEADLINE);
String date = bun.getString(KEY_ARTICLEDATE);
String Body = bun.getString(KEY_BODY);
//String image = bun.getString(KEY_IMAGEURL);
Bitmap image = (Bitmap) bun.getParcelable(KEY_IMAGEURL);
  WebView browser = (WebView)findViewById(R.id.webView1);

String HTML = ......................;
browser.loadData(HTML, "text/html", "utf-8"); 

这是我的应用程序的logcat

          12-16 16:10:46.017: E/dalvikvm(19543): GC_FOR_ALLOC freed 1537K, 19% free 10221K/12579K, paused 4ms+5ms
          12-16 16:10:46.017: I/dalvikvm-heap(19543): Grow heap (frag case) to 10.719MB for 660976-byte allocation
          12-16 16:10:46.064: E/dalvikvm(19543): GC_FOR_ALLOC freed <1K, 19% free 10866K/13283K, paused 2ms+2ms
          12-16 16:10:46.118: D/webviewglue(19543): nativeDestroy view: 0x9683f0
          12-16 16:10:46.126: D/webhistory(19543): Writing Form data else {} Tag
          12-16 16:10:46.126: D/webhistory(19543): Writing Form data else {} Tag
          12-16 16:10:46.173: I/dalvikvm(19602): Turning on JNI app bug workarounds for target SDK version 8...

2 个答案:

答案 0 :(得分:0)

避免使用此buildDrawingCache()方法 根据android开发者文档

Build Drawing Cache

调用此方法会增加内存使用量并导致视图在软件中呈现一次,从而对性能产生负面影响。 尝试另一种方法将图像网址或图像数据传递到另一个活动

答案 1 :(得分:0)

试试这个..

像这样改变您的位图编码..

BitmapDrawable bitmapDrawable = ((BitmapDrawable) thumb_image.getDrawable());
Bitmap bitmap1 = bitmapDrawable .getBitmap();      

示例

thumb_image.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                 String title = ((TextView) findViewById(R.id.textURL)).getText().toString();
                 String date = ((TextView) findViewById(R.id.dateURL)).getText().toString();
                 String body = ((TextView) findViewById(R.id.bodyURL)).getText().toString();
                 BitmapDrawable bitmapDrawable = ((BitmapDrawable) thumb_image.getDrawable());
                 Bitmap image = bitmapDrawable.getBitmap();      
                 Bundle extras = new Bundle();
                 extras.putParcelable(KEY_IMAGEURL, image);
                 // Starting new intent
                 Intent i = new Intent(this, SingleMenuItemActivity.class);
                 i.putExtra(KEY_HEADLINE, title);
                 i.putExtra(KEY_ARTICLEDATE, date);
                 i.putExtra(KEY_BODY,body);
                 i.putExtras(extras);

                 startActivity(i);
            }
});