我正在尝试使用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);
}
答案 0 :(得分:0)
你将一个String传递给setImageResource(),它需要一个整数。
资源ID实际上是整数 - 当您在代码中引用R.drawable.myImage时,这指的是由编译器创建的自动生成的代码(R.java)中的整数资源。
从资源字符串加载图片(不推荐)请参阅Android - Open resource from @drawable String
答案 1 :(得分:0)
这适用于图像ID而非图像字符串
如果您想在imageview中显示图片,请执行以下操作:
lblImage.setImageResource(R.drawable.image);
OR
根据您的要求,您必须从图像链接中获取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;
}
}
调用此函数并将set drawable设置为图像视图。
Drawable image = LoadImageFromWebOperations(Image); // Image means iamge url getting
from previous activity
imageview.setImageDrawable(image);