# \[Android\] ListView imageview - How should I load the image file?

**URL:** <https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650>\
**Category:** Processing for Android\
**Created:** [April 8, 2023, 8:32am UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650 "2023-04-08T08:32:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [April 8, 2023, 8:32am UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650/1 "2023-04-08T08:32:22Z")

</div>

[Android] List image view - How should I load the image file?

- List example site: [https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/ListView.txt](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/ListView.txt)  
(Thanks to Dear noel, EmmanuelPil.)

```auto
import android.app.Activity;
import android.widget.FrameLayout;     
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.View.OnClickListener;
import android.R;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.AdapterView;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.content.Context;
import android.view.LayoutInflater;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.widget.TextView;
import android.view.ViewGroup;
import android.graphics.BitmapFactory;
import android.os.Environment;

// import android.widget.RelativeLayout.LayoutParams;
//import android.app.Activity;
//import android.graphics.Color;
//import android.os.Bundle;
//import android.view.View;
//
//

String[] myStringArray = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", 
  "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifteenth"};

Activity act;
FrameLayout fl;
ListView listView;
int padding_left;
int padding_right;
int padding_bottom;
int padding_top;

PImage img;
String image_folder = "list_image_view";
String url = ""; // new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";

void findImage(String sf, String tf) { 
  try { 
    String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath()); 
    File file = new File(absolute_path+"/"+sf+"/"+tf); 
    println("file : " + file);
    if (!file.exists()) { 
      // boolean success = true; 
      // success = file.mkdirs();
      println("File not find.");
    }else{
      println("File find.");
    }
    
    
    
  } 
  catch (Exception e) { 
    println("Error : " + e);  
  }
}

void setup() {

  
  
  println("step-0-1");
  // url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/image.png";  

  act = this.getActivity();  
  println("step-0-2");
  
  
  // img = loadImage(url);
  
  url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
  println(" url : " + url);
  findImage(image_folder,"image.png");
  
  
  
  
  
  fullScreen();
  background(255, 190, 10);
  fill(0);
  textAlign(CENTER, CENTER);
  String str = "Click on screen to show listView.";
  textSize(12);
  float twf = width/textWidth(str);
  textSize(9*twf);
  text(str, width/2, height/2);
  println("step-0-3");
  
}

void draw() {
  
  
}

void mousePressed() {
  
  act.runOnUiThread(
    new Runnable() {
    public void run() {
      background(255, 190, 10);
      pushStyle();
      stroke(160, 60, 0);
      strokeWeight(20);
      noFill();
      rectMode(CENTER);
      rect(width/2, height/2, width-2*padding_left, height-2*padding_top);
      popStyle();
      listView.setVisibility(View.VISIBLE);
    }
  }
  );
  
}

//@ Override 
public void onStart() {
  
  super.onStart();
  act = this.getActivity();
  padding_left = width/6;
  padding_right = width/6;
  padding_bottom = height/6;
  padding_top = height/6;
  ArrayAdapter adapter = new ArrayAdapter<String>(act, android.R.layout.simple_list_item_1, myStringArray);
  listView = new ListView(act);
  listView.setLayoutParams(new RelativeLayout.LayoutParams(width-padding_left-padding_right, height-padding_top-padding_bottom));
  listView.setAdapter(adapter);
  listView.setBackgroundColor(Color.rgb(255, 230, 130));
  listView.setX(padding_left);
  listView.setY(padding_top);
  //listView.setPadding(padding_left, padding_top, padding_right, padding_bottom);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    // @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      Object list = listView.getItemAtPosition(position);
      background(255, 190, 10);
      text("You have chosen item: "+list, width/2, height/2);
      text("Click to call list again.", width/2, height/2+100);
      listView.setVisibility(View.GONE);
    }
  }
  );
  listView.setVisibility(View.GONE);
  fl = (FrameLayout)act.findViewById(R.id.content);
  fl.addView(listView);

  MyAdapter adapter2 = new MyAdapter(act, myStringArray);
  listView.setAdapter(adapter2);
  
  
}

private class MyAdapter extends ArrayAdapter<String> {

  public MyAdapter(Context context, String[] values) {
    super(context, android.R.layout.simple_list_item_1, values);
  }
  

  

  // Override
    public View getView(int position, View convertView, ViewGroup parent) {
      
    System.out.println("step-1");
  //try {
  // url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
  // System.out.println("step-2 "+url);
  //}catch (Exception e) { } 
      
    LayoutInflater inflater = LayoutInflater.from(getContext());
    View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    System.out.println("step-3");

    TextView textView = view.findViewById(android.R.id.text1);
    textView.setText(getItem(position));

    ImageView imageView = new ImageView(getContext());
    AssetManager am = getResources().getAssets() ;
    InputStream is = null ;
    System.out.println("step-4"); 

        try {
            // 애셋 폴더에 저장된 field.png 열기.
    System.out.println("step-5"); 
            is = am.open(url) ; 
            Bitmap bm = BitmapFactory.decodeStream(is);
    System.out.println("step-6");
            // 만들어진 Bitmap 객체를 이미지뷰에 표시.
            imageView.setImageBitmap(bm) ;
            is.close() ;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (is != null) {
            try {
                is.close() ;
            } catch (Exception e) {
                e.printStackTrace() ;
            }
        }
    
    // imageView = (ImageView) findViewById(R.id.image1) ;
    
    // imageView.setImageResource(bm);
    // imageView.setImageResource(img);
    
    imageView.setLayoutParams(new LinearLayout.LayoutParams(100, 100)); // set the size of the image here

    LinearLayout layout = (LinearLayout) view;
    layout.addView(imageView);

    return view;
  }
}

```

```auto
url : /storage/emulated/0/list_image_view/image.png
file : /storage/emulated/0/list_image_view/image.png
File not find.

```

there is a problem.

1. processing(android) folder / data / image\_sample.png
2. I need to read ‘image\_sample.png’, but I don’t know where the file is located.

Is there any way to find it?

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [April 8, 2023, 2:24pm UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650/2 "2023-04-08T14:24:26Z")

</div>

I was unable to run the posted code. It’s pretty simple if all you want is an image; just place the image in a ‘data’ folder inside of your sketch folder.

```auto
PImage img;

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(209);
  // Needs to be inside 'data' folder
  img = loadImage("myImage.jpg");
}

void draw() {
  image(img, 480, 480, 300, 300);
}

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [April 8, 2023, 7:28pm UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650/3 "2023-04-08T19:28:37Z")

</div>

@svan

Dear svan

Nice to meet you. thank you

‘Bitmap bm’ An image must be assigned to the corresponding variable.  
How can I put a picture file in ‘Bitmap’ in ‘Processing’?

The answer is here.

> [@PDF export for Android sketch](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738):
>
> Hi everyone! I am making an app that does some sort of calculations and draws something with the result. Now I want to export a pdf of the same. Although my purpose is satisfied on a computer with pdf export library, but I’m not able to find any pdf library for android. Also, the pdf need not be the same thing as the screen, so screenshot would not work out. I’m new to processing, and any help will be appreciated slight_smile

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [April 8, 2023, 8:04pm UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650/4 "2023-04-08T20:04:48Z")

</div>

```auto
import android.app.Activity;
import android.widget.FrameLayout;     
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.View.OnClickListener;
import android.R;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.AdapterView;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.content.Context;
import android.view.LayoutInflater;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.widget.TextView;
import android.view.ViewGroup;
import android.graphics.BitmapFactory;
import android.os.Environment;

// import android.widget.RelativeLayout.LayoutParams;
//import android.app.Activity;
//import android.graphics.Color;
//import android.os.Bundle;
//import android.view.View;
//
//

String[] myStringArray = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", 
  "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifteenth"};

Activity act;
FrameLayout fl;
ListView listView;
int padding_left;
int padding_right;
int padding_bottom;
int padding_top;

PImage img;
String image_folder = "list_image_view";
String url = ""; // new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";

String cheminComplet="";
String imageName="";

void findImage(String sf, String tf) { 
  try { 
    String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath()); 
    File file = new File(absolute_path+"/"+sf+"/"+tf); 
    println("file : " + file);
    if (!file.exists()) { 
      // boolean success = true; 
      // success = file.mkdirs();
      println("File not find.");
    }else{
      println("File find.");
    }
    
    
    
  } 
  catch (Exception e) { 
    println("Error : " + e);  
  }
}

void setup() {

  
  
  println("step-0-1");
  // url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/image.png";  

  act = this.getActivity();  
  println("step-0-2");
  
  
   
  // url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
  // println(" url : " + url);
  // findImage(image_folder,"image.png");
  
  img = loadImage("image.png");
  File externalDir = Environment.getExternalStorageDirectory();
  String path = externalDir.getAbsolutePath();
  cheminComplet = path+File.separator;
  imageName = "image_sample.png";
  img.save(path+File.separator+imageName);
  
  
  
  fullScreen();
  background(255, 190, 10);
  fill(0);
  textAlign(CENTER, CENTER);
  String str = "Click on screen to show listView.";
  textSize(12);
  float twf = width/textWidth(str);
  textSize(9*twf);
  text(str, width/2, height/2);
  println("step-0-3");
  
}

void draw() {
  
  
}

void mousePressed() {
  
  act.runOnUiThread(
    new Runnable() {
    public void run() {
      background(255, 190, 10);
      pushStyle();
      stroke(160, 60, 0);
      strokeWeight(20);
      noFill();
      rectMode(CENTER);
      rect(width/2, height/2, width-2*padding_left, height-2*padding_top);
      popStyle();
      listView.setVisibility(View.VISIBLE);
    }
  }
  );
  
}

//@ Override 
public void onStart() {
  
  super.onStart();
  act = this.getActivity();
  padding_left = width/6;
  padding_right = width/6;
  padding_bottom = height/6;
  padding_top = height/6;
  ArrayAdapter adapter = new ArrayAdapter<String>(act, android.R.layout.simple_list_item_1, myStringArray);
  listView = new ListView(act);
  listView.setLayoutParams(new RelativeLayout.LayoutParams(width-padding_left-padding_right, height-padding_top-padding_bottom));
  listView.setAdapter(adapter);
  listView.setBackgroundColor(Color.rgb(255, 230, 130));
  listView.setX(padding_left);
  listView.setY(padding_top);
  //listView.setPadding(padding_left, padding_top, padding_right, padding_bottom);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    // @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      Object list = listView.getItemAtPosition(position);
      background(255, 190, 10);
      text("You have chosen item: "+list, width/2, height/2);
      text("Click to call list again.", width/2, height/2+100);
      listView.setVisibility(View.GONE);
    }
  }
  );
  listView.setVisibility(View.GONE);
  fl = (FrameLayout)act.findViewById(R.id.content);
  fl.addView(listView);

  MyAdapter adapter2 = new MyAdapter(act, myStringArray);
  listView.setAdapter(adapter2);
  
  
}

private Bitmap createBitmap(String in){////could be better to create an handler for that
 
  Bitmap b = null;
  b = BitmapFactory.decodeFile(cheminComplet+in); 
return b;
}

private class MyAdapter extends ArrayAdapter<String> {

  public MyAdapter(Context context, String[] values) {
    super(context, android.R.layout.simple_list_item_1, values);
  }
  

  

// Override
public View getView(int position, View convertView, ViewGroup parent) {
      
    System.out.println("step-1");
  //try {
  // url = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+image_folder)+"/image.png";
  // System.out.println("step-2 "+url);
  //}catch (Exception e) { } 
      
    LayoutInflater inflater = LayoutInflater.from(getContext());
    View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    System.out.println("step-3");

    //TextView textView = view.findViewById(0); // android.R.id.text1
    //textView.setText(getItem(position));

    ImageView imageView = new ImageView(getContext());
    AssetManager am = getResources().getAssets() ;
    InputStream is = null ;
    System.out.println("step-4"); 

        try {
            // 애셋 폴더에 저장된 field.png 열기.
    System.out.println("step-5"); 
            // is = am.open(url) ; 
            // is = am.open("image.png") ;             
            // Bitmap bm = BitmapFactory.decodeStream(is);
            
            Bitmap bm = createBitmap(imageName) ;
            if( bm == null ) { println(" not image!"); } 
            
    System.out.println("step-6");
            // 만들어진 Bitmap 객체를 이미지뷰에 표시.
            imageView.setImageBitmap(bm) ;

            // is.close() ;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (is != null) {
            try {
                is.close() ;
            } catch (Exception e) {
                e.printStackTrace() ;
            }
        }
    System.out.println("step-7");
    // imageView = (ImageView) findViewById(R.id.image1) ;    
    // imageView.setImageResource(bm);
    // imageView.setImageResource(img);
    
    imageView.setLayoutParams(new LinearLayout.LayoutParams(100, 100)); // set the size of the image here
    System.out.println("step-8");
    LinearLayout layout = (LinearLayout) view;
    System.out.println("step-9");
    layout.addView(imageView);
    System.out.println("step-10");
    
    
    return view;
  }
}

```

An execution error pops up.

> FATAL EXCEPTION: main  
> Process: processing.test.list\_image\_view, PID: 19185  
> java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.LinearLayout  
> at processing.test.list\_image\_view.list\_image\_view$MyAdapter.getView(list\_image\_view.java:308)  
> at android.widget.AbsListView.obtainView(AbsListView.java:3219)  
> at android.widget.ListView.makeAndAddView(ListView.java:2219)

 ![eer](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/5/6520dc75f274225728dea89814a423b710b8e291.jpeg)

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [April 10, 2023, 5:35am UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650/5 "2023-04-10T05:35:50Z")

</div>

link: [android - how to create listview ui like this - Stack Overflow](https://stackoverflow.com/questions/41057348/how-to-create-listview-ui-like-this)

 ![aa](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/3/83e35f727ef7193b08d394293a58e01fd9cc9c1e.jpeg)

I want to implement ‘image listview’ through ‘processing android’. Does anyone know?

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [April 10, 2023, 6:03am UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650/6 "2023-04-10T06:03:07Z")

</div>

Have you seen this demo: [https://github.com/vsquared/AndroidWidgets\_Processing4](https://github.com/vsquared/AndroidWidgets_Processing4) . There is a listView demonstrated here and should be customizable.

It’s also possible to create xml code with JavaFX; I have no experience (yet). SceneBuilder (free) by Gluon will also create xml code for mobile devices (drag and drop).

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [April 10, 2023, 10:01pm UTC](https://discourse.processing.org/t/android-listview-imageview-how-should-i-load-the-image-file/41650/7 "2023-04-10T22:01:48Z")

</div>

@svan

Thank you for the good content (source code).
