Android 通过使用Intent将GridView图像(从URL)发送到第二个活动
首先,我给出了我目前使用的代码 主要活动Android 通过使用Intent将GridView图像(从URL)发送到第二个活动,android,bitmap,android-imageview,Android,Bitmap,Android Imageview,首先,我给出了我目前使用的代码 主要活动 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.setOnItemClickListene
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);
}
});
在这种情况下,如果我像上面那样使用代码,标题
起作用,我可以在txt中看到文本,但我无法获得img。我想我需要把它转换成位图,但它也不适合我。为了转换位图,我使用了这个
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);
但在解码资源行之后的主要活动中,它给出了以下错误:
BitmapFactory类型中的decodeResource(Resources,int)方法不适用于参数(Resources,String)
如果您能提供帮助,我将非常高兴。您正在使用字符串作为BitmapFactory.decodeResource()中的第二个参数 但是根据您的代码,位图的创建应该是这样的
位图bMap=BitmapFactory.decodeResource(getResources(),R.drawable.yourImageId) 是的,但我的IM ID必须是可绘制的,但来自internet且未按名称保存的图像,它们正在等待/保留在缓存中,因此使用此代码时,我无法使用r.drawable.ID,因为不存在。为此,请使用以下代码。。位图位图=BitmapFactory.decodeByteArray(bitmapdata,0,bitmapdata.length);
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);