我怎样才能将点击的图像传递给另一个类
我有一个问题,我现在还无法解决,这个问题可能对你们中的一些人来说很简单,但我实在无法进一步处理。 我暂时将数据库中的对象存储在数组List中,并且可以将这些对象作为列表视图显示在我的应用程序中。不过,我需要将点击的项目传递给另一个活动。 我试图通过这个如下。我怎样才能将点击的图像传递给另一个类
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,
View view,int position, long id) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(),
Property.class);
in.putExtra("temp_image", image);
in.putExtra("temp_identifier", identif);
in.putExtra("temp_bedrooms", bedrooms);
in.putExtra("temp_address", address);
in.putExtra("temp_propType", propType);
startActivity(in);
}});
和从另一个活动
identif = getIntent().getExtras().getStringArrayList("temp_identifier");
bedrooms = getIntent().getExtras().getStringArrayList("temp_bedrooms");
address = getIntent().getExtras().getStringArrayList("temp_address");
propType = getIntent().getExtras().getStringArrayList("temp_propType");
image = getIntent().getExtras().getParcelable("temp_image");
img = (ImageView) view.findViewById(R.id.image);
img.setImageBitmap(BitmapFactory.decodeByteArray(
image.get(POSITION), 0,
image.get(POSITION).length));
上述否则接收具有两个挫折。
1)返回一个数组列表对象(未含)
2)我无法通过图片
我怎么只显示点击的数组列表项目和其他活动显示。
编辑后的版本:强大的文本
**Here is how i'm querying the images!**
private void getDataAndPopulate() {
// TODO Auto-generated method stub
image = new ArrayList<byte[]>();
identif = new ArrayList<String>();
price= new ArrayList<String>();
bedrooms= new ArrayList<String>();
address= new ArrayList<String>();
propType= new ArrayList<String>();
Cursor cursor = getEvents(" gall,properties where properties._id =
gall._id ");
//Select* from gall,properties where properties.propertyId = gall.GallId
while (cursor.moveToNext()) {
String temp_id = cursor.getString(0);
byte[] temp_image = cursor.getBlob(2);
String temp_identifier = cursor.getString(1);
String temp_price = cursor.getString(3);
String temp_bedrooms = cursor.getString(4);
String temp_address = cursor.getString(5);
String temp_propType = cursor.getString(6);
image.add(temp_image);
identif.add(temp_identifier);
price.add(temp_price);
bedrooms.add(temp_bedrooms);
address.add(temp_address);
propType.add(temp_propType);
}
和getDataAndPopulate()是
private Cursor getEvents(String table) {
SQLiteDatabase db = (placeData).getReadableDatabase();
Cursor cursor = db.query(table, null, null, null, null, null, null);
return cursor;
}
在这里,我ASING他们适配器。
String[] identifierArray = (String[]) identif.toArray(new String[identif
.size()]);
ItemsAdapter itemsAdapter = new ItemsAdapter(Result.this,
R.layout.item, identifierArray);
setListAdapter(itemsAdapter);
在此先感谢
束具有非常小的限制,这就是为什么你收到此失败的粘结剂交易,你试图传递太多的位图超过捆绑的最大尺寸。既然你是从数据库中获得这些信息,为什么不传递图像的主键并从下一个活动中再次从数据库中检索图像呢?
Bitmap s为Parcelable,所以你可以把他们当临时演员。从您的绘制使用得到的位图下面的代码:
if(myImage instanceof BitmapDrawable) { // myImage is a Drawable
BitmapDrawable bitmapDrawable = (BitmapDrawable) myImage;
Bitmap bedroomBitmap = bitmapDrawable.getBitmap();
Intent intent = new Intent();
intent.putExtra("bedroom", bedroomBitmap);
...
}
要转换的byte []使用此:
Bitmap bedroomBitmap = BitmapFactory.decodeByteArray(bedroomBytes, 0, bedroomBytes.length);
intent.putExtra("bedroom", bedroomBitmap);
,图像来了一个数据库作为字节[],我可以将它们显示为列表我只是想将它们传递给另一个活动将这样做的工作?? – SomCollection 2012-03-07 13:40:45
查看已更新的答案。 – 2012-03-07 13:47:31
使用BitmapFactory,然后使用intent.putExtra获取图像的位图( )并放置该位图。在下一个活动中,使用getIntent()。getParcelableExtra()获取该位图,然后使用image.setImageBitmap()将该位图设置为图像。
多数民众赞成正是我所做的,但我越来越!!!!失败的粘合剂交易从logCat – SomCollection 2012-03-07 13:45:32
代码你粘贴有图像,这是什么,它是一个位图或imageView参考 – 2012-03-07 13:47:54
检查我的问题更新请。 – SomCollection 2012-03-07 13:57:00
转换图像的base64字符串通过故意发送,然后再转换为字符串的位图文件,这会做的伎俩,它为我工作
如
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
encode = Base64.encodeBytes(byteArray);
byte[] decode = Base64.decode(encode);
Bitmap bmp = BitmapFactory.decodeByteArray(decode, 0,decode.length);
imgview_photo.setImageBitmap(bmp);
你能告诉我一些代码吗? – SomCollection 2012-03-07 13:46:22
有一个类将图像转换为base64编码,这里是我的代码的一部分,以从相机获取图像,然后使用base64将变量转换为字符串,我使用变量“encode”通过肥皂发送 – 2012-03-07 13:52:09
您需要一个外部Android版本的Base64编解码器至2.1。它可以从API Level 8(Android 2.2)及以上版本获得。但恕我直言,这不是一个好的做法,将字节转换为字符串,然后转换为位图。为什么你需要做很多转换? – 2012-03-07 13:52:32
通过意图可以传递可串行化仅限物件。
图像/绘图类不是可串行化的。所以你不能直接通过Intent传递它们。
我觉得这个链接有更好的解决方案:
how-do-you-pass-images-bitmaps-between-android-activities-using-bundles
Ragunath Jawahar,感谢有用的回应我同意我传递一组错误的图像数组。我的目标是只传递点击的图像,这应该是较少的位图处理,但我无法在此论坛上找到任何解决方案或google.How我可以通过该图像点击到另一个活动? – SomCollection 2012-03-08 10:19:59
我想这里是我的问题,String [] identifierArray =(String [])identif.toArray(new String [identif .size()]); ItemsAdapter adapter = new ItemsAdapter(Property.this,R.layout.item, identifierArray); – SomCollection 2012-03-08 10:22:22
你是如何查询数据库中的图像的? – 2012-03-08 10:41:18