GridView控件图片(从URL)至次活动使用意图 [英] GridView Image (from URL) to Second Activity by using Intent
问题描述
所有IM给予codeS我使用什么目前首先
First of all im giving the codes what i'm using currently
MainActivity
MainActivity
static final String URL = "http://my .com/images/rss.xml";
static final String KEY_TITLE = "item";
static final String KEY_THUMB_URL = "thumb_url";
// Click event for single list row
gridView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map2 = (HashMap<String, String>) parent.getItemAtPosition(position);
Intent in = new Intent(getApplicationContext(), FullSize.class);
Bitmap b; // your bitmap
in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));
in.putExtra(KEY_THUMB_URL, KEY_THUMB_URL);
startActivity(in);
}
});
第二届活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullsize);
ImageView image = (ImageView) findViewById(R.id.fullsizeimg2);
TextView txtName = (TextView) findViewById(R.id.txt1);
Intent in = getIntent();
// Receiving the Data
String name = in.getStringExtra("item");
Bitmap bitmap =(Bitmap) in.getParcelableExtra("thumb_url");
// Displaying Received data
txtName.setText(name);
image.setImageBitmap(bitmap);
}
}
在这种情况下,如果我用codeS如上所述,标题
作品中,我可以看到TXT文本,但我不能得到IMG。我想我需要将其转换为位图,而且它没有为我工作。转换位图我用这个
in this case, if i use the codes as above , the title
works , i can see the text in txt but i cannot get img. i think i need to convert it to bitmap but also it didnt work for me. for converting bitmap i used this
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),"Image ID");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
////////for intent /////
intent.putExtra("imagepass", bytes.toByteArray());
/////////2nd activity//////////
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("imagepass");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView iv=(ImageView) findViewById(R.id.fullsizeimg);
iv.setImageBitmap(bmp);
但在德coderesource行后的主要活动,这是给这个错误:
在类型BitmapFactory的方法去codeResource(资源,INT)不适用于参数(资源字符串)
but in main activity after decoderesource line, it was giving this error : The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, String)
我会很高兴,如果你可以提供帮助。
I will be very happy if you can help.
推荐答案
您正在使用字符串作为BitmapFactory.de codeResource第二个参数()
但是,根据您的code中的位图创作应该是这样的
位图BMAP = BitmapFactory.de codeResource(getResources(),R.drawable.yourImageId);
You are using String as the second parameter in BitmapFactory.decodeResource() But according to your code the Bitmap creation should be like this Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.yourImageId);
这篇关于GridView控件图片(从URL)至次活动使用意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!