0

我正在尝试使用 listview 项将图像从一个活动传递到另一个活动,但我收到此错误行

方法 setImageResource(int)

是图像视图类型不适用于参数(字符串),并且在这一点上: -

lblImage.setImageResource(图像);

我现在不知道需要写什么来消除此错误请有人支持我并编写适合此行的确切代码,在这里我还放置一些代码供您参考:

    SingleMenuItem(Activity to fetch Image with some text data)::

   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);
    String Image = in.getStringExtra(KEY_THUMB_URL);
    //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);
    ImageView lblImage = (ImageView) findViewById(R.id.image_label);

    //image.setImageBitmap(bitmap); 

    lblName.setText(title);
    lblCost.setText(artist);
    lblDesc.setText(duration);
    lblImage.setImageResource(Image); //Getting Error at this line only
    //the method setImageResource(int) is the type Image View is not
    // applicable for the argument(string)
    }
    }

    ActivityCode (to pass image and text data)
            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[] array = null;
              // Bitmap thumb_url = BitmapFactory.decodeByteArray
                            (array, 0, array.length);
            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);
            //in.putExtra(KEY_THUMB_URL, thumb_url);
            startActivity(in);

        }
4

2 回答 2

0

您将一个字符串传递给 setImageResource(),这需要一个整数。

资源 ID 实际上是整数 - 当您在代码中引用 R.drawable.myImage 时,这是指由编译器创建的自动生成代码 (R.java) 中的整数资源。

要从资源字符串加载图像(不推荐),请参阅Android - Open resource from @drawable String

于 2012-09-27T05:43:31.197 回答
0

这将适用于图像 id 而不是图像字符串

如果您想在 imageview 中显示图像,请执行以下操作:

 lblImage.setImageResource(R.drawable.image);

或者

根据您的要求,您必须从 imag 链接中获取 Drawable 。通过使用此代码,您可以获得可绘制的图像,然后设置为图像视图

public static Drawable LoadImageFromWebOperations(String url) {
   try {
      InputStream is = (InputStream) new URL(url).getContent();
      Drawable d = Drawable.createFromStream(is, "src name");
      return d;
   } catch (Exception e) {
      return null;
   }
}

像这样调用此函数并将可绘制对象设置为图像视图。

Drawable image = LoadImageFromWebOperations(Image); // Image means iamge url getting                                     
                                                       from previous activity
imageview.setImageDrawable(image);
于 2012-09-27T05:44:57.463 回答