n4h1n
n4h1n

Reputation: 357

Cant get ImageView onItemClick from custom ListView Android [EDIT]

I've implemented a custom ListView, this is the structure of each row:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imagenEvento"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:adjustViewBounds="false"
android:paddingBottom="2dp"     
android:contentDescription="@string/contenidoImagen"       
/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:padding="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titulo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/sitio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/fecha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

I've setted to my ListView and adapter and it works fine while accesing data, but when i try to convert an ImageView to a byteArray so i can send it to another Activitie, i can't get the ImageView

lv = (ListView) findViewById(R.id.lista);       
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            //creo el nuevo objeto para poder llamarlo
            Intent intent = new Intent(AllProductsActivity.this, PostActivity.class);

            //Creo la informacion para pasar entre actividades
            Bundle informacion= new Bundle();
            informacion.putInt("id", eventos.get(position).getID());

            Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            informacion.putByteArray("imagen", byteArray);                              
        }

    });

I'm getting my bmp NULL:

Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);

I guess i should indicate the position of the item due it's in a ListView but i don't know how to do it

[EDIT] The problem is solved, i'm getting the image in the next Activity, but, i'm getting always the top image from the ListView of this Activity, how can i get the clicked Image?

Upvotes: 1

Views: 980

Answers (2)

Girish Nair
Girish Nair

Reputation: 5216

The ImageView that your using is just a reference so no need to convert it into an array just pass the reference directly as static Or make a static class that holds such references so that you can use it later, But also you can save the image of the imageview as bitmap and encode save that image as byte array and decode it when needed, and later use it imageView as imageView.setImageBitmap(bitmap);

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();


public String BitmapToString(Bitmap bitmap){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
    byte[] b = baos.toByteArray();
    String image = Base64.encodeToString(b, Base64.DEFAULT);
    return image;
}


public Bitmap StringToBitmap(String image){
    byte[] encodeByte =  Base64.decode(image, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0 , encodeByte.length);
    return bitmap;
}

Upvotes: 0

metalmonkeysoftware
metalmonkeysoftware

Reputation: 636

Instead of:

Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);

to get your bitmap image. Try this:

ImageView image = (ImageView)v.findViewByID(R.id.iamgeenEvento);
Bitmap bmp = ((BitmapDrawable)image.getDrawable()).getBitmap();

Your original code was trying to decode the imageview itself as a bitmap and as it's not a bitmap would return a null value.

Upvotes: 1

Related Questions