0

I have an issue which I cannot solve at this point, this issue might be very simple to some of you but I’m really can not proceed further. I’m temporarily storing objects from database in an array List and I can display these objects in my app as list view. However I need to pass just the Item clicked to another activity. I have tried to pass this as follows.

            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);

        }});

And receiving from the other Activity

        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));

Doing the above has two downfalls.

1) Returns a an Array List objects(Text only)

2) I cannot pass the Image

How do I show just the clicked Array List Item and display it in another activity.

Edited Version:strong text

  **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);


           } 

and the getDataAndPopulate() is

            private Cursor getEvents(String table) {
    SQLiteDatabase db = (placeData).getReadableDatabase();
    Cursor cursor = db.query(table, null, null, null, null, null, null);    
    return cursor;

}

Here i'm asing them to adapter.

     String[] identifierArray = (String[]) identif.toArray(new String[identif
                                                                    .size()]);
     ItemsAdapter itemsAdapter = new ItemsAdapter(Result.this,
   R.layout.item, identifierArray);
   setListAdapter(itemsAdapter);

Thanks in advance

SomCollection
  • 481
  • 1
  • 7
  • 17

5 Answers5

1

The bundle has a very small limit, that is why you are getting this Failed Binder Transaction, you are trying to pass too many bitmaps which exceed the maximum size for the bundle. Since you are getting this from the database, why not pass the primary keys of the images and retrieve the image from the database again from the next activity?

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
  • Ragunath Jawahar,thanks for helpfull response I do agree that I'm passing an array of images which is wrong.My aim is to only pass the image clicked and this should be less bitmap to process but I cannot find any solution on this forum or google.How can I pass just that Image clicked to another activity? – SomCollection Mar 08 '12 at 10:19
  • I think here is my problem, String[] identifierArray = (String[]) identif.toArray(new String[identif .size()]); ItemsAdapter adapter = new ItemsAdapter(Property.this, R.layout.item, identifierArray); – SomCollection Mar 08 '12 at 10:22
  • How are you querying for your images from the database? – Ragunath Jawahar Mar 08 '12 at 10:41
  • I have edited the question to show you my querying process.please have a look at the code part. thanks – SomCollection Mar 08 '12 at 10:51
  • This would have been easier if you'd have created a model already. Now, create an ArrayList to hold the primary keys and when the user clicks on a list item, pass the primary key to the next activity. Then from the extra, fetch the primary key and re-query your database and then you'll be able to retrieve all the required images. – Ragunath Jawahar Mar 08 '12 at 11:56
0

Bitmaps are Parcelable, so you can send them as extras. To get a bitmap from your drawable use the following code:

if(myImage instanceof BitmapDrawable) { // myImage is a Drawable
    BitmapDrawable bitmapDrawable = (BitmapDrawable) myImage;
    Bitmap bedroomBitmap = bitmapDrawable.getBitmap();

    Intent intent = new Intent();
    intent.putExtra("bedroom", bedroomBitmap);

    ...
}

To convert your byte[] use this:

Bitmap bedroomBitmap = BitmapFactory.decodeByteArray(bedroomBytes, 0, bedroomBytes.length);
intent.putExtra("bedroom", bedroomBitmap);
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
  • ,The image is coming a database as byte[] and I can display them as list I just want to pass them to another activity will this do the job?? – SomCollection Mar 07 '12 at 13:40
0

Get the bitmap of the image using BitmapFactory, then use intent.putExtra() and put that bitmap. In the next activity get that bitmap using getIntent().getParcelableExtra() and then set this bitmap to the image using image.setImageBitmap().

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
0

Convert image to base64 string send via intent and then again convert the string to bitmap file, this will do the trick, it works for me

eg

 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);
Maulik J
  • 2,745
  • 19
  • 22
  • there's a class to conver a image to base64 encoding, here's a part of my code to get image from camera and then convert it to string using base64, the variable "encode" i am using to send through soap – Maulik J Mar 07 '12 at 13:52
  • You will need a external Base64 codec for Android versions till 2.1. It is available from API Level 8 (Android 2.2) and above. But IMHO this is not a good practice, converting bytes to string and then to a bitmap. Why do you have to do so many conversions? – Ragunath Jawahar Mar 07 '12 at 13:52
0

Through Intent You can pass Serializable Objects Only.

Image/Drawable Classes are not Serializable. So you can't pass them through Intent directly.

I think this Link has Better Solution :

how-do-you-pass-images-bitmaps-between-android-activities-using-bundles

Community
  • 1
  • 1
Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31